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编程思想(二)一切都是对象
2.1用句柄操纵对象 尽管一切都看作是对象,但是操纵的标识符实际上是指向一个对象的“句柄”(handdle): 拥有一个句柄并不表示必须有一个对象同他连接: String s: 这里创建的只是句 ...
- Design Compressed String Iterator
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- SQL SERVER CONVERT函数
定义: CONVERT函数返回 转换了数据类型的数据. 语法: CONVERT(target_type,expression,date_style smallint) 参数: ①target_type ...
- 设置springmvc全局异常
设置全局异常,将异常信息指定内容展示给前端页面,保证程序的安全性 @Slf4j@Componentpublic class ExceptionResolver implements HandlerEx ...
- kmp跑两串的最大相同前后缀 HDU2594
题意:http://acm.hdu.edu.cn/showproblem.php?pid=2594 如题. 思路: Next数组记录的是pos位置失配时要跑到哪里,所以最后得再添加一个字符‘#’. 连 ...
- Photon Server初识(五) --- 客户端连接服务端
准备:Unity开开发IDE. 一.新建Unity3D项目 导入包,在资源下新建目录 Plugins .在之前解压的SDK目录 lib中找到 Photon3Unity3D.dll,拖到新建的目前下 二 ...
- session和cookie区别,多台WEB服务器如何共享session,禁用COOKIE后SESSION是否可用,为什么?
答:session的运行机制: 用户A访问站点Y,如果站点Y指定了session_start();(以下假设session_start()总是存在)那么会产生一个session_id,这个sessio ...
- MyBatis 示例-主键回填
测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...
- hdu 1281 匈牙利算法
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HTML Ueditor加载空白问题
问题描述 Ueditor打开时加载不出内容 原因分析 Ueditor重复加载时,会存在缓存问题 Ueditor采用异步加载方式,所以数据获取和赋值要写在Ueditor异步回调里 解决方案 UE.del ...