How to click a button through coding?
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.S开发者_C百科etAttribute("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;
}
}
}
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.
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();
}
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(...);
}
精彩评论