当我单击表单(winform)中的按钮时,我想显示一个网页(google)。
我已经尝试了以下代码,但对我不起作用.....
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应用程序
任何人都可以帮忙.....
提前谢谢了...
最佳答案
首先在表单中添加一个按钮,然后在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");
}
关于c# - 在按钮上单击表单即可打开网页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7414847/