Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms
If this sounds stupid don't down vote it please, answer it and I'll probably accept it.
 

So here's my problem
public void btnOk_Click(object sender, EventArgs e)
{
    //when btnOk is clicked execute this
}
public void btnOk2_Click(object sender, EventArgs e)
{
    btnOk_Click(/*I don't know what to enter here for 'object sender, EventArgs e' to get to 'btnOk_Click'*/);
    
}
I know I could do this
 
btnOk2.Click += new EventHandler(btnOk_Click);
but that doesn't work for what I want.
Any suggestions?
Posted 10-Nov-12 5:33am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

An important question is: Are you using the parameters in btnOk_Click?
If no, then do this:
public void btnOk2_Click(object sender, EventArgs e)
{
     btnOk_Click(null,null);
}
If yes, then you need to call the method with the necessary parameters.
  Permalink  
v3
Comments
Curtdawg123 at 10-Nov-12 13:32pm
   
Okay, thanks, that makes sense
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
 
You seem to need to execute a common code for two ButtonClick events. I think the best way is to write the code to be executed in a separate method. Let's say the method be Common_Code(). Then do the following:
 
btnOk.Click += new EventHandler(btnOk_Click);
btnOk2.Click += new EventHandler(btnOk2_Click);
 
Then do this:
 
public void btnOk_Click(object sender, EventArgs e)
{
    this.Common_Code();
}
public void btnOk2_Click(object sender, EventArgs e)
{
    this.Common_Code();
 
}
 

While calling Common_Code() method use appropriate parameters as you might be needing. The this keyword helps you to point at the current object through which you are calling the method.
 
I think this will be better way to execute a common code.
  Permalink  
Comments
Curtdawg123 at 10-Nov-12 13:34pm
   
I can see what you mean, but in my program that doesn't quite fit.
Dave Kreskowiak at 10-Nov-12 14:18pm
   
Yes, it does fit. There is never a reason to have one evnet handler call another.
 
If there are other parameters that have to be passed, you can change the method header to cover those. But, since you haven't spelled out why "it doesn't fit", it's impossible to tell you how to make it work.
Ritwesh at 11-Nov-12 3:06am
   
If you can tell us why it 'doesn't fit' then we can probably try to find some other way to it.
Curtdawg123 at 17-Nov-12 10:50am
   
Look at 'ProgramFox's way of doing it that was what I was looking for. Sorry if I didn't explain how it didn't fit. I'm pretty new to all this.
Ritwesh at 17-Nov-12 14:52pm
   
Glad you got what you wanted buddy...:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

For the 'sender' argument, you can also use the keyword 'this', meaning that the form itself.
  Permalink  
Comments
Curtdawg123 at 10-Nov-12 13:32pm
   
Okay

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

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100