简体   繁体   English

在按钮上单击表单即可打开网页

[英]open a web page on button click in form

I want show a web page (google) when i click on the button in form(winform).... 当我单击表单(winform)中的按钮时,我想显示一个网页(google)。

I have tried the below code but it does not work for me..... 我已经尝试了以下代码,但对我不起作用.....

   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();
    }
}

I am doing winforms application using c# 我正在使用C#进行Winforms应用程序

would any one pls help on this..... 任何人都可以帮忙.....

many thanks in advance... 提前谢谢了...

First Add a button to your form and in the Click event handler do this 首先在表单中添加一个按钮,然后在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");
}
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;   
    } 
}

On Click of button add: 在单击按钮上添加:

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

(or) (要么)

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

If you want to use web scraping technique to extract data from the particular website or open the website in your winform itself then use web browser control and try anyone of the answers provided by ShaliniPavan or V4Vendetta. 如果要使用Web抓取技术从特定网站提取数据或在Winform本身中打开网站,请使用Web浏览器控件并尝试ShaliniPavan或V4Vendetta提供的任何答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM