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. [转帖]Oracle 起诉 Google 事件

    Oracle 起诉 Google 事件 https://www.cnblogs.com/panchanggui/p/9449842.html Oracle 是世界第二大软件公司 世界第一大DBMS公司 ...

  2. python接口测试

    引入unittest框架 get请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #coding: utf-8 import un ...

  3. 导出远程oracle数据库到本地

    1.以管理员身份运行 Net Manager 以管理员身份运行cmd

  4. superset部署

    superset功能概述: 丰富的数据可视化集 易于使用的界面,用于探索和可视化数据 创建和共享仪表板 与主要身份验证提供程序集成的企业级身份验证(通过Flask AppBuilder进行数据库,Op ...

  5. ubuntu18上传代码到github

    其实在github上建仓库时候就提示你步骤了: 1.注册个github账号并登录 创建一个仓库 https://github.com/ 2.创建SSH Key ssh-keygen -t rsa -C ...

  6. Web安全小结之前端

  7. 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...

  8. python学习-1 编程语言的介绍

    1.开发语言: 高级语言:Java.python.C#.PHP.Go.ruby.C++等等 低级语言:C.汇编语言 2.机器码和字节码 高级语言 ====>字节码 低级语言===>机器码 ...

  9. ~ android与ios的区别

    Oracle与Mysql的区别 项目类别 android ios 应用上 可以使用常用的android模拟器,来模拟各种android设备 只能直接使用iphone或ipad进行测试 开发语言 基于L ...

  10. Pygame小游戏练习二

    @Python编程从入门到实践 Python项目练习 四.创建Ship类 建立ship.py,创建Ship类,管理飞船行为. # ship.py import pygame class Ship(): ...