【问题标题】:open a web page on button click in form在按钮上打开网页单击表单
【发布时间】:2011-09-14 20:36:20
【问题描述】:

当我点击表单(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 应用程序

有没有人帮忙解决这个问题.....

提前非常感谢...

【问题讨论】:

  • 105 : 您没有在按钮点击中提供任何链接
  • 为什么没有在点击事件中设置url?
  • @V4Vendetta 我不知道怎么做,我只是尝试了上面一个...
  • 你为什么要投反对票

标签: c# .net winforms buttonclick


【解决方案1】:

首先在表单中添加一个按钮,然后在 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");
}

【讨论】:

    【解决方案2】:
    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;   
        } 
    }
    

    【讨论】:

      【解决方案3】:

      点击按钮添加:

      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 提供的任何答案。

      【讨论】:

      • 至少看看提供的代码,他有一个网络浏览器控件。然后他尝试将其导航到所需的网站。
      猜你喜欢
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2014-12-18
      • 2010-12-21
      • 1970-01-01
      相关资源
      最近更新 更多