1

当我单击表单(winform)中的按钮时,我想显示一个网页(谷歌)......

我已经尝试了下面的代码,但它对我不起作用......

   public partial class Form1 : Form {
    bool mHooked;
    public Form1() {
        InitializeComponent();
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        webBrowser1.Navigate("http://www.google.com");
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (mHooked) return;
        // Get the form
        HtmlDocument doc = webBrowser1.Document;
        HtmlElement form = doc.Forms["f"];
        // Get the "I'm feeling lucky" button
        HtmlElement lucky = form.All["btnI"];
        lucky.Click += lucky_Click;
        mHooked = true;
    }
    void lucky_Click(object sender, EventArgs e) {
        this.Close();
    }
}

我正在使用 c# 做 winforms 应用程序

有人可以帮忙吗.....

提前谢谢了...

4

3 回答 3

4

首先在表单中添加一个按钮,然后在 Click 事件处理程序中执行此操作

private void button1_Click(object sender, EventArgs e)
{           
   //remove this from the constructor else it will be loaded along with the form
    webBrowser1.Navigate("http://www.google.com");
}
于 2011-09-14T10:47:31.997 回答
4
public partial class Form1 : Form

{

    bool mHooked;

    public Form1()

    {
        InitializeComponent();
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        //webBrowser1.Navigate("http://www.google.com"); 
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.google.com"); 
    }
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    {
        if (mHooked) return;   
        // Get the form 
        HtmlDocument doc = webBrowser1.Document; 
        HtmlElement form = doc.Forms["f"];    
        // Get the "I'm feeling lucky" button 
        HtmlElement lucky = form.All["btnI"];
        lucky.Click += button1_Click;   
        mHooked = true;   
    } 
}
于 2011-09-14T10:50:57.173 回答
0

单击按钮添加:

ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");  
Process.Start(sInfo);

(或者)

System.Diagnostics.Process.StartProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");
Process.Start(sInfo);

如果您想使用网络抓取技术从特定网站中提取数据或在您的 winform 中打开该网站,请使用网络浏览器控件并尝试由 ShaliniPavan 或 V4Vendetta 提供的任何答案。

于 2011-09-14T12:13:06.337 回答