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使用 网络(二)的更多相关文章

  1. C# 通过扩展WebBrowser捕获网络连接错误信息

    想捕获WebBrowser连接指定网站过程中发生的错误信息,包括网络无法连接.404找不到网页等等错误!经过网上的搜集,找到了以下解决方案,该解决方案不会在网站连接前发出多余的测试请求. 向Webbr ...

  2. 办公室网络二三事 - chunyu

    开始的时候,我们办公室拉了两条家庭百兆宽带,两条宽带分别接到路由器的wan1/wan2口上,我们愉快的工作愉快的上网. 后来,再拉了一条十兆企业专线,接到了路由器的wan3口上面,配了一些静态路由,希 ...

  3. 机器学习:深入理解LSTM网络 (二)

    之前我们介绍了RNN 网络结构以及其所遇到的问题,RNN 结构对于关联度太长的时序问题可能无法处理, 简单来说,RNN对于太久远的信息不能有效地储存,为了解决这个问题,有人提出了LSTM的网络结构,L ...

  4. Linux的VMWare下Centos7的三种网络配置过程(网络二)

    Linux之VMWare下Centos7的三种网络配置过程 环境:虚拟软件:VMWare 14.0客户机:windows 10虚拟机:centos 7 VMware三种网络连接方式 Bridge(桥接 ...

  5. 【卷二】网络二—TCP服务器与客户端

    经过上回简单地介绍,大家对服务器多少应该清楚一些了吧!还记得TCP: (Transmission Control Protocol) 传输控制协议? 还记得IP: (Internet Protocol ...

  6. 网络二十四题 之 P2756 飞行员配对方案问题

    题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...

  7. socket网络(二)

    作用域 python/js语言中,无块级作用域 if 1 == 1: name = 'alex' print(name) python中以函数为作用域 def func(): name = 'alex ...

  8. 【CentOS】虚拟机网络配置与远程登录

    ////////////////////////////////////11月16日更新////////////////////////////////////////////////////// 一 ...

  9. 【网络】VPN

    VPN: 来自百度百科 虚拟专用网络的功能是:在公用网络上建立专用网络,进行加密通讯.在企业网络中有广泛应用.VPN网关通过对数据包的加密和数据包目标地址的转换实现远程访问.VPN有多种分类方式,主要 ...

随机推荐

  1. Hadoop集群搭建-01前期准备

    Hadoop集群搭建-05安装配置YARN Hadoop集群搭建-04安装配置HDFS  Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hado ...

  2. DB2部分查询SQL

    /* 部分SQL */ --添加主键 alter TABLE TABLE_SCHEMA.TABLE_NAME add constraint PK_TABLE_NAME primary key(COL1 ...

  3. 和HTTP相关的web服务器内容

    一台web服务器可以搭建多个独立域名的网站,也可以作为通信路径上的中转服务器提升传输效率. 1. 用单台虚拟主机实现多个域名 多个域名解析后对应的ip地址相同,需要在Host首部中包含完整的主机名或者 ...

  4. Scala当中什么是Transformation和 Action,以及它们俩的区别是什么?

    [学习笔记] 一个完整的RDD任务由两部分组成:Transformation和 Action.Transformation用于对RDD的创建,还可以把老的RDD通过Transformation来生成新 ...

  5. 数据结构:BF算法

    贴上源代码: #include<iostream> using namespace std; int BF(char S[],char T[]) { int i,j; i = j = 0; ...

  6. centos7.2 安装Lnmp

    1. 安装编译工具及库文件 yum install -y make apr* autoconf automake curl  \ curl-devel gcc gcc-c++ cmake gtk+-d ...

  7. Typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  8. 按Excel的模板导出数据

    思路:把Excel模板上传到服务器:然后读取文档,写入数据,另存为新的文件 然后就是导出的时候处理数据定义的字符串,按数据导出:注意读取的数据流要处理下,不然会报异常 public Stream Ge ...

  9. Node.js学习(2)-使用模板引擎art-template

    node 安装cnpm i -S art-template 加载require('art-template') template.render接收的是字符串

  10. webapi 之 post参数传递

    最近在写webapi,在写post请求接口时遇到了不少的问题,在此记录下来. post请求的参数和get请求有点不一样,我们知道get请求的参数是通过url来传递的,而post请求则是通过http的请 ...