Windows Develop Bookmark and Share   
 index > Windows Forms General > Button question
 

Button question

Hi all!

I have two buttons on my form. myButton01 and myButton02.

When myButton01 is clicked, it calls the method private void hitBtn_Click(object sender, EventArgs e). This works fine.

On the second button I placed the Click property to the same method as above.

In this method for myButton02, I wanted to add additionalcode to the methodrather then making a duplicate codes for myButton02.

So, I guess my question is, how can I tell if myButton02 is clicked in this method?

Any help would be appreciated.

Dave

dpsolsys  Tuesday, December 11, 2007 3:12 PM

You don't. (*)

Code Block

private void hitBtn2_Click(object sender, EventArgs e)

{

hitBtn_Click(sender, e); // call handler for btn 1

// Additional stuff for button 2 here.

}

(*) Yes, what you ask can be done. But it shouldn't be.
James Curran  Tuesday, December 11, 2007 4:11 PM

If I understood you correctly, you can use the following:

private void hitBtn_Click(object sender, EventArgs e)

{

Button temp_btn = ( Button)sender;

// code for both buttons

if(temp_btn.Name ==Btn2.Name)

{

//additional code for Button2

}

}

danych  Tuesday, December 11, 2007 4:32 PM

if(temp_btn.Name ==Btn2.Name)

The reason I wanted to encourage doing it at compile-time instead of run-time, is because run-time methods lead to bad designs:

The "correct" way of doing that:

Code Block

private void hitBtn_Click(object sender, EventArgs e)

{

// code for both buttons

if (sender ==Btn2)

{

//additional code for Button2

}

}

James Curran  Tuesday, December 11, 2007 7:55 PM

You don't. (*)

Code Block

private void hitBtn2_Click(object sender, EventArgs e)

{

hitBtn_Click(sender, e); // call handler for btn 1

// Additional stuff for button 2 here.

}

(*) Yes, what you ask can be done. But it shouldn't be.
James Curran  Tuesday, December 11, 2007 4:11 PM

Thanks James,

I started writing my code to your suggested path and that seems to work.

Should I always avoidthe otherlogic?

and curiously, how would the other way be known?

dpsolsys  Tuesday, December 11, 2007 4:30 PM

If I understood you correctly, you can use the following:

private void hitBtn_Click(object sender, EventArgs e)

{

Button temp_btn = ( Button)sender;

// code for both buttons

if(temp_btn.Name ==Btn2.Name)

{

//additional code for Button2

}

}

danych  Tuesday, December 11, 2007 4:32 PM

Should I always avoidthe otherlogic?

Because it has you doing things at run-time which can & should be done at compile-time.

James Curran  Tuesday, December 11, 2007 4:32 PM

Hi dan!

Yes, this is what I was looking for. I tried both ways of programming and they both worked.

Many of my questions are very, very beginner level in this forum. So, anything that will make me understand better is greatly appreciated.

Thanks and another question will arrive soon enough.

Dave

dpsolsys  Tuesday, December 11, 2007 7:47 PM

if(temp_btn.Name ==Btn2.Name)

The reason I wanted to encourage doing it at compile-time instead of run-time, is because run-time methods lead to bad designs:

The "correct" way of doing that:

Code Block

private void hitBtn_Click(object sender, EventArgs e)

{

// code for both buttons

if (sender ==Btn2)

{

//additional code for Button2

}

}

James Curran  Tuesday, December 11, 2007 7:55 PM

You can use google to search for other answers

Custom Search

More Threads

• force a windows form lose focus
• hashtable key
• error provider
• plug to check spilling mistake the commnt in Source Code project
• Puting the progress events in a seperate Class
• Picture above main menu control ????
• Listbox test
• How to specify precision for a datacolumn
• Misadventures in Printing
• MaskedTextBox firing Leave event when '0' is entered?