C# WebBrowser使用 网络(二)
WebBrowser 简单操作
Form 代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char))
{
webBrowser1.Navigate(textBox1.Text);
}
} private void Form1_Load(object sender, EventArgs e)
{
buttonBack.Enabled = false;
buttonForward.Enabled = false;
buttonStop.Enabled = false; this.webBrowser1.CanGoBackChanged += (s, k) => { if (webBrowser1.CanGoBack) buttonBack.Enabled = true; else buttonBack.Enabled = false; };
this.webBrowser1.CanGoForwardChanged += (s, k) => { if (webBrowser1.CanGoForward) buttonForward.Enabled = true; else buttonForward.Enabled = false; };
this.webBrowser1.DocumentTitleChanged += (s, k) => { this.Text = webBrowser1.DocumentTitle.ToString(); };
} private void buttonBack_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
textBox1.Text = webBrowser1.Url.ToString();
} private void buttonForward_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
textBox1.Text = webBrowser1.Url.ToString();
} private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
this.Text = webBrowser1.DocumentTitle.ToString();
textBox1.Text = webBrowser1.Url.ToString();
} private void buttonStop_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
} private void buttonHome_Click(object sender, EventArgs e)
{
webBrowser1.GoHome();
textBox1.Text = webBrowser1.Url == null ? "" : webBrowser1.Url.ToString();
} private void buttonRefresh_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
} private void buttonSubmit_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
} private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
buttonStop.Enabled = true;
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textBox2.Text = webBrowser1.DocumentText;
buttonStop.Enabled = false;
if (webBrowser1.CanGoBack)
buttonBack.Enabled = true;
else
buttonBack.Enabled = false; if (webBrowser1.CanGoForward)
buttonForward.Enabled = true;
else
buttonForward.Enabled = false;
} private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Print();
}
}
Designer 代码
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.buttonSubmit = new System.Windows.Forms.Button();
this.buttonRefresh = new System.Windows.Forms.Button();
this.buttonHome = new System.Windows.Forms.Button();
this.buttonStop = new System.Windows.Forms.Button();
this.buttonForward = new System.Windows.Forms.Button();
this.buttonBack = new System.Windows.Forms.Button();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(, );
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(, );
this.textBox1.TabIndex = ;
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// buttonSubmit
//
this.buttonSubmit.Location = new System.Drawing.Point(, );
this.buttonSubmit.Name = "buttonSubmit";
this.buttonSubmit.Size = new System.Drawing.Size(, );
this.buttonSubmit.TabIndex = ;
this.buttonSubmit.Text = "Submit";
this.buttonSubmit.UseVisualStyleBackColor = true;
this.buttonSubmit.Click += new System.EventHandler(this.buttonSubmit_Click);
//
// buttonRefresh
//
this.buttonRefresh.Location = new System.Drawing.Point(, );
this.buttonRefresh.Name = "buttonRefresh";
this.buttonRefresh.Size = new System.Drawing.Size(, );
this.buttonRefresh.TabIndex = ;
this.buttonRefresh.Text = "Refresh";
this.buttonRefresh.UseVisualStyleBackColor = true;
this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click);
//
// buttonHome
//
this.buttonHome.Location = new System.Drawing.Point(, );
this.buttonHome.Name = "buttonHome";
this.buttonHome.Size = new System.Drawing.Size(, );
this.buttonHome.TabIndex = ;
this.buttonHome.Text = "Home";
this.buttonHome.UseVisualStyleBackColor = true;
this.buttonHome.Click += new System.EventHandler(this.buttonHome_Click);
//
// buttonStop
//
this.buttonStop.Location = new System.Drawing.Point(, );
this.buttonStop.Name = "buttonStop";
this.buttonStop.Size = new System.Drawing.Size(, );
this.buttonStop.TabIndex = ;
this.buttonStop.Text = "Stop";
this.buttonStop.UseVisualStyleBackColor = true;
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// buttonForward
//
this.buttonForward.Location = new System.Drawing.Point(, );
this.buttonForward.Name = "buttonForward";
this.buttonForward.Size = new System.Drawing.Size(, );
this.buttonForward.TabIndex = ;
this.buttonForward.Text = "Forward";
this.buttonForward.UseVisualStyleBackColor = true;
this.buttonForward.Click += new System.EventHandler(this.buttonForward_Click);
//
// buttonBack
//
this.buttonBack.Location = new System.Drawing.Point(, );
this.buttonBack.Name = "buttonBack";
this.buttonBack.Size = new System.Drawing.Size(, );
this.buttonBack.TabIndex = ;
this.buttonBack.Text = "Back";
this.buttonBack.UseVisualStyleBackColor = true;
this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(, );
this.webBrowser1.MinimumSize = new System.Drawing.Size(, );
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(, );
this.webBrowser1.TabIndex = ;
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
this.webBrowser1.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webBrowser1_Navigated);
this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(, );
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(, );
this.textBox2.TabIndex = ;
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "Print";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.textBox2);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonSubmit);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonRefresh);
this.Controls.Add(this.buttonHome);
this.Controls.Add(this.buttonStop);
this.Controls.Add(this.buttonForward);
this.Controls.Add(this.buttonBack);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button buttonSubmit;
private System.Windows.Forms.Button buttonRefresh;
private System.Windows.Forms.Button buttonHome;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonForward;
private System.Windows.Forms.Button buttonBack;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
}
}
通过流 读取页面源代码
WebClient client = new WebClient();
Stream str= client.OpenRead("http://www.baidu.com");
StreamReader sr = new StreamReader(str); string line;
while ((line=sr.ReadLine())!=null)
{
listBox1.Items.Add(line);
}
str.Close();
打印方法
webBrowser1.Print();
C# WebBrowser使用 网络(二)的更多相关文章
- C# 通过扩展WebBrowser捕获网络连接错误信息
想捕获WebBrowser连接指定网站过程中发生的错误信息,包括网络无法连接.404找不到网页等等错误!经过网上的搜集,找到了以下解决方案,该解决方案不会在网站连接前发出多余的测试请求. 向Webbr ...
- 办公室网络二三事 - chunyu
开始的时候,我们办公室拉了两条家庭百兆宽带,两条宽带分别接到路由器的wan1/wan2口上,我们愉快的工作愉快的上网. 后来,再拉了一条十兆企业专线,接到了路由器的wan3口上面,配了一些静态路由,希 ...
- 机器学习:深入理解LSTM网络 (二)
之前我们介绍了RNN 网络结构以及其所遇到的问题,RNN 结构对于关联度太长的时序问题可能无法处理, 简单来说,RNN对于太久远的信息不能有效地储存,为了解决这个问题,有人提出了LSTM的网络结构,L ...
- Linux的VMWare下Centos7的三种网络配置过程(网络二)
Linux之VMWare下Centos7的三种网络配置过程 环境:虚拟软件:VMWare 14.0客户机:windows 10虚拟机:centos 7 VMware三种网络连接方式 Bridge(桥接 ...
- 【卷二】网络二—TCP服务器与客户端
经过上回简单地介绍,大家对服务器多少应该清楚一些了吧!还记得TCP: (Transmission Control Protocol) 传输控制协议? 还记得IP: (Internet Protocol ...
- 网络二十四题 之 P2756 飞行员配对方案问题
题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...
- socket网络(二)
作用域 python/js语言中,无块级作用域 if 1 == 1: name = 'alex' print(name) python中以函数为作用域 def func(): name = 'alex ...
- 【CentOS】虚拟机网络配置与远程登录
////////////////////////////////////11月16日更新////////////////////////////////////////////////////// 一 ...
- 【网络】VPN
VPN: 来自百度百科 虚拟专用网络的功能是:在公用网络上建立专用网络,进行加密通讯.在企业网络中有广泛应用.VPN网关通过对数据包的加密和数据包目标地址的转换实现远程访问.VPN有多种分类方式,主要 ...
随机推荐
- Java学习路径
-------第一部分:基础语法-------- 1.输出语句 1.1 hello world 1.2 拼接输出.换行和不换行输出 1.3 拼接变量输出 2.输入语句: 2.1 定义变量,赋值(整数. ...
- Web文件上传靶场 - 通关笔记
Web应用程序通常会提供一些上传功能,比如上传头像,图片资源等,只要与资源传输有关的地方就可能存在上传漏洞,上传漏洞归根结底是程序员在对用户文件上传时控制不足或者是处理的缺陷导致的,文件上传漏洞在渗透 ...
- java实现4种内部排序
两种插入类排序: 直接插入排序: public static void insertSort(int[] arr, int len){ for(int i=1; i<len; i++){ int ...
- ggpubr进行“paper”组图合并,也许比PS,AI更简单
本文转载自微信公众号 “生信补给站”,https://mp.weixin.qq.com/s/41iKTulTwGcY-dHtqqSnLA 多个图形进行组图展示,可以既展示一个“事情”的多个角度,也可以 ...
- php--正则(手机号码)
PHP手机号码正则表达式 php用正则表达式判断手机号码的写法:从文章中匹配出所有的手机号就可以preg_match_all(),如果要检查用户输入的手机号是否正确可这样来检查:preg_match( ...
- Javascript的学习清单
Javascript的学习清单 Javascript学习资源 程序员必读书籍 深入理解JavaScript系列 es6教程 jQuery中文文档 vue官网 zeptojs中文版 常用的插件与UI组件 ...
- react快速上手一(使用js语法,创建虚拟DOM元素)
1.装包,引包 首先需要安装两个包 react ,react-dom cnpm i react react-dom 介绍下这两个包: react:专门用来创建React组件,组件生命周期等这些东西. ...
- oracle索引查询
/*<br>* *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'table ...
- ASE2019 model组 事后诸葛亮会议记录
诸葛亮文档 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 传统编程教育模式下,初学者(主要是刚刚接触编程的学生)往往依靠老师与助教的课堂教学,由 ...
- C++手动调用析构函数无效问题排查
在学习C++的时候,都知道不要手动调用析构函数,也不要在构造函数.析构函数里调用虚函数.工作这么多年,这些冷门的知识极少用到,渐渐被繁杂的业务逻辑淹没掉. 不过,最近项目里出现了析构函数没有被正确地调 ...