执行与单一的点击两个按钮 [英] Execute two buttons with single click

查看:283
本文介绍了执行与单一的点击两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含按钮的简单的C#应用​​程序和网络浏览器,每个按钮执行请求,并显示在网络浏览器的请求的结果。并利用一个请求的结果中的下一个按钮。

I have a simple C # application that contains buttons and a web browser, each button execute a request and displays the result of the request on the web browser. And use the results of a request in the next button.

private void button1_Click(object sender, EventArgs e)
        {
webBrowser1.Navigate("http://www.test.com");
}

private void button2_Click(object sender, EventArgs e)
        {
            if (webBrowser1.Document.GetElementById("tesxtbox1") != null)
            {
            HtmlElement txt1 = webBrowser1.Document.GetElementById("tesxtbox1");
            txt1.SetAttribute("value", "test");
            webBrowser1.Document.Forms[0].InvokeMember("submit");
            }
        }

有没有任何方法或与一个单一的点击执行两个按钮的方式,但第二个按钮,直到Web浏览器加载不执行。

Is there any method or way to perform the two buttons with a single click, but the second button, do not execute until the web browser is loaded.

在第一个按钮,我添加

webBrowser1.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (Button2_Click);

但第二个按钮excuted几次,所以在最后一行添加的按钮2:

but the second button excuted several times, so I added in the last line of the button 2:

webBrowser1.DocumentCompleted - = new WebBrowserDocumentCompletedEventHandler (Button2_Click);

它的工作原理,但在控制台中我发现,按钮2次执行

it works, but in the console I find that Button 2 is execute twice

在此先感谢!

推荐答案

您应该添加将调用code,一旦该文件已完成加载的事件。

You should add an event that will call the code once the document has completed loading.

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}    

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    button2_Click(sender, e);
}

这篇关于执行与单一的点击两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆