How to click a button through coding?

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I have two buttons in my program and i want that when i press first button the second button is clicked automatically ( in the event handler of first button , i want to press the second button through coding).

private void button1_Click(object sender, EventArgs e)     {          passWord = pwd.Text;         user = uName.Text;           loginbackend obj = new loginbackend();         bool isValid = obj.IsValidateCredentials(user, passWord, domain);         if (isValid)         {             loginbackend login = new loginbackend();             passWord = pwd.Text;              login.SaveUserPass(passWord);             HtmlDocument webDoc = this.webBrowser1.Document;             HtmlElement username = webDoc.GetElementById("__login_name");             HtmlElement password = webDoc.GetElementById("__login_password");              username.SetAttribute("value", user);             password.SetAttribute("value", passWord);              HtmlElementCollection inputTags = webDoc.GetElementsByTagName("input");              foreach (HtmlElement hElement in inputTags)             {                 string typeTag = hElement.GetAttribute("type");                 string typeAttri = hElement.GetAttribute("value");                  if (typeTag.Equals("submit") && typeAttri.Equals("Login"))                 {                     hElement.InvokeMember("click");                      break;                 }             }             button3_Click(sender, e);             label1.Visible = false ;             label3.Visible = false;             uName.Visible = false;             pwd.Visible = false;             button1.Visible = false;             button2.Visible = true;     }          else          {             MessageBox.Show("Invalid Username or Password");         }      } private void button3_Click(object sender, EventArgs e)     {         HtmlDocument webDoc1 = this.webBrowser1.Document;         HtmlElementCollection aTags = webDoc1.GetElementsByTagName("a");          foreach (HtmlElement link in aTags)         {             if (link.InnerText.Equals("Show Assigned"))             {                 link.InvokeMember("click");                 break;             }         }     }

回答1:

I think what you're describing is that you want to call a method when button B is clicked, but then also call that method when button A is clicked.

protected void ButtonA_Click(...) {     DoWork(); }  protected void ButtonB_Click(...) {     // do some extra work here     DoWork(); }  private void DoWork() {     // do the common work here }

Depending on your implementation in the event handlers, you can also just call the event handler of the second button from that of the first, but the above way is the 'right' way to do it.



回答2:

You could just call the method.

    private void btnA_Click(object sender, EventArgs e)     {         doA();     }     private void doA()     {         //A stuff     }      private void btnB_Click(object sender, EventArgs e)     {         doA();         doB();     }     private void doB()     {         //B stuff     }

Or call the _Click method directly;

    private void btnB_Click(object sender, EventArgs e)     {         btnA_Click(sender, e);         doB();     }


回答3:

I guess, you don't really care whether the button is clicked or not, you just care that the code of the second button is executed. So... just call it:

void button1_Click(...) {     button2_Click(...); }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!