Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
How to call one button click event in another
Posted
Updated 29-Mar-17 8:31am
Comments
walterhevedeich 1-Aug-11 3:24am    
In another what? Provide more details to your question if you want specific answers. A question like this will only bring you assumptions, which might not be helpful to you on your end.

Don't. Instead, set up a private method that performs the function and call it from both event handlers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 4:16am    
Sure, my 5. It is not even possible, the way it was literally asked. The event can only be raised in the class where it is declared.
--SA
Member 10311687 1-Mar-16 2:01am    
nice
BobJanova 1-Aug-11 5:55am    
Quite.
You might try below,

C#
namespace EventHandlerCall
{
    using System;
    using System.Windows.Forms;

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            btnOne.Click += new EventHandler(common_Click);
            btnTwo.Click += new EventHandler(common_Click);
        }

        private void common_Click(object sender, EventArgs e)
        {
            CommonFuncationality();
        }

        private void CommonFuncationality()
        {
            MessageBox.Show("Hello world");
        }
    }
}


:)
 
Share this answer
 
C#
private void btnA_Click(object sender, EventArgs e)
{
    btnB_Click(sender, e);
}
private void btnB_Click(object sender, EventArgs e)
{
    ExecuteFunction();
}


But best practice is:
C#
private void btnA_Click(object sender, EventArgs e)
{
   ExecuteFunction();
}
private void btnB_Click(object sender, EventArgs e)
{
   ExecuteFunction();
}
private void ExecuteFunction()
{
    // Your code here
}

Credit goes to here
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 4:20am    
You explained the idea, but even better practice would be 1) not violating Microsoft naming conventions using names with '_' which are also non-semantic, 2) by using anonymous methods calling semantic methods (such as ExecuteFunction, to be named semantically). Overall, my 4.
--SA
Monjurul Habib 1-Aug-11 4:27am    
yes, i agree. thank you.
BobJanova 1-Aug-11 5:57am    
You can blame Microsoft for the conventions for event handler method names, the 'control_EventName' is what you get if you let its autocomplete do its thing. I'm not quite sure what you mean by 'semantic' names for event handlers, though – 'this method handles the X event of Y' seems okay on that front to me. If the event causes some complex calculation to happen which should have a different name, that should be in another method (quite possibly not even in the UI layer) anyway.
Uday P.Singh 2-Aug-11 7:56am    
my 5! :)
Monjurul Habib 2-Aug-11 14:19pm    
thank you.
You can use Button2.PerformClick() in the Button1 click event handler.
 
Share this answer
 
HI,

1) take two buttons on a webform.
2) Double click on both the button to generate their click events and then write the following :


C#
Button1(object sender ,Eventargs e)
{
Response.write("i am button1");
Button2_click(Button2, null);
}

Button2(object sender ,Eventargs e)
{
Response.write("i am button2");
}



Gud luck.
 
Share this answer
 
v5
Comments
OriginalGriff 1-Aug-11 4:27am    
Reason for my vote of one: That is a bad idea, for two reasons.
Firstly because you should not call event handlers directly, only from their delegate. If you must simulate a click on a different button, then use the Button.PerformClick method instead - this causes all events chained to the button click event to be fired.

Secondly because it could cause the Button2 event handler to crash. Since you have an EventArgs parameter, pass it through, or create a new instance. Do not pass a null, as very little (if any) code I have ever seen checks for null EventArgs - no real event will pass null through, so it is not considered necessary.
Member 10623273 30-Mar-14 5:23am    
Thank You Buddy.
this Small Code Help me a lot..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900