hi,

i have a form in which there are two buttons.. i ve written code for the button 1 and button 2 in the click event. now i want to click button 2 programmtically. that is when button1 is clicked it should execute the set of coding also it should invoke the button2 to be clicked.

anybody help me to do this task.. how can i click the button2 from button1.

thanks in advance.

Recommended Answers

All 13 Replies

private void button1(object sender, EventArgs e)
{
            .......//ur button1 code
            button2(sender, e);   //calls button2
}
private void button2(object sender, EventArgs e)
{
            .......//ur button 2 code
}

This will call the button2 function
Its as if you have pressed the actual button.

thanks for ur reply ,

but that also doesnt work..

actually i ve used like this,

private void button1_Click(object sender, EventArgs e) 
        { 
            webBrowser1.Navigate("http://xxx/login.aspx"); 
            this.button2_Click(sender, e);      
       
        } 
        private void button2_Click(object sender, EventArgs e) 
        { 
            webBrowser1.document.GetElementById("userlogin").InnerText = "xxxxx"; 
            webBrowser1.document.GetElementById("passwd").InnerText = "yyyyyy"; 
            webBrowser1.document.GetElementById("loginbutton").InvokeMember("click"); 
            button3.PerformClick(); 
        }

but its not working.. need help!

thanks,
expecting ur reply.

Take away the "this."

private void button1_Click(object sender, EventArgs e)
{
      webBrowser1.Navigate("http://xxx/login.aspx");
      button2_Click(sender, e);
 }
 private void button2_Click(object sender, EventArgs e)
 {
     webBrowser1.document.GetElementById("userlogin").InnerText = "xxxxx";
     webBrowser1.document.GetElementById("passwd").InnerText = "yyyyyy";
     webBrowser1.document.GetElementById("loginbutton").InvokeMember("click");
     button3.PerformClick();
}

that should work...

The suggestion made by ICode is accurate if you stay within the same page between buttons. Since you navigate away from the current page, the call to the click will fail due to the fact that you are no longer on that page and button2 is no longer in the current context. You can get around this by extracting new methods that the button events call and call them instead of trying to call the button click event.

private void button1_Click(object sender, EventArgs e)         
{             
   webBrowser1.Navigate("http://xxx/login.aspx");             
   button2Actions();           
}         

private void button2_Click(object sender, EventArgs e)
{
   button2Actions();
 }

private void button2Actions()
{
    //ur button 2 code
}

I was able to reproduce the error you were getting using a windows form app and changing from form 1 to form 2 after the first button click and attempting to access the second button. Extracting methods fixed this by removing the link to the actual button, that may or may not exist in your current context and just executes the code that the click would execute normally. Give that a try and let me know if that helps. I guess it may work differently in a web app.

You can try this:

#
private void button1_Click(object sender, EventArgs e)
{

webBrowser1.Navigate("http://xxx/login.aspx");

button2.PerformClick();

}

private void button2_Click(object sender, EventArgs e)

{

hi,

i have a form in which there are two buttons.. i ve written code for the button 1 and button 2 in the click event. now i want to click button 2 programmtically. that is when button1 is clicked it should execute the set of coding also it should invoke the button2 to be clicked.

anybody help me to do this task.. how can i click the button2 from button1.

thanks in advance.

you can do this it very simple..

public void btn1_Click(Object sender,EventArgs e)
{
      btn2_Click(sender, e);
}
public void btn2_Click(Object sender,EventArgs e)
{
         your code present here
}

thanks for replying psi-josh

private void button1_Click(object sender, EventArgs e)         
{             
   webBrowser1.Navigate("http://xxx/login.aspx");             
   button2Actions();           
}         

private void button2_Click(object sender, EventArgs e)
{
   button2Actions();
 }

private void button2Actions()
{
    //ur button 2 code
}

i ve tried your code. but unfortunately it doesnt work.. and one thing i want to explain u. in my form there are 4 buttons and one web browser control. i need to click the button1 and then the button2 till button4. every button has a certain set of code which should be executed in sequence.[that is one after other]. so only i need that code for a button to be clicked in another button event.

now i ve explained clear i think..

need ur help.

thanks all for responding and showing interest,

but the code and ideas u provide here does not help me.. it doesnt work.. so pls any other suggestion?

expecting reply.

Thats odd!!!!
Are you sure they're all in the sme class????
because if they werent then you should make the button functions public instead of private.
but usually the calling always works like that if both are in the same .cs file and same class

Try creating a method which contains instructions of button1, then invoke that method when clicking button1 and when clickin button2 also, like this:

private void button1_Click(object sender, EventArgs e) 
        { 
            webBrowser1.Navigate("http://xxx/login.aspx"); 
            this.button2_Actions();    
 
        } 
        private void button2_Click(object sender, EventArgs e) 
        { 
           this.button2_Actions();
        }
		
        private void button2_Actions(){
		     webBrowser1.document.GetElementById("userlogin").InnerText = "xxxxx"; 
            webBrowser1.document.GetElementById("passwd").InnerText = "yyyyyy"; 
            webBrowser1.document.GetElementById("loginbutton").InvokeMember("click"); 
		}

thats how i used before as " psi-josh " said.. but thats also does not work.. any other suggestion?

expecting reply.

Are button1 and button2 in the same class?

s.. two buttons are in the same class only

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.