形式

2011-09-14 26 views
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應用

將在此任何一個請幫助.....

許多在此先感謝...

+0

105:您還沒有提供任何鏈接按鈕點擊 – 62071072SP

+0

爲什麼你不在點擊事件中設置網址? – V4Vendetta

+0

@ V4Vendetta我不知道如何纔剛剛嘗試過一個...... –

回答

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"); 
} 
+0

非常感謝它的工作現在.. –

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; 
    } 
} 
0

在點擊按鈕的添加:

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

(或)

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

如果你想使用網絡抓取技術從特定網站提取數據或打開e網站,然後使用Web瀏覽器控制並嘗試ShaliniPavan或V4Vendetta提供的任何答案。

+1

至少看看提供的代碼,他有一個webbrowser控件。他試圖將其導航到所需的網站。 – Aidiakapi