Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click event for more than 1 button

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
Hello,

VS 2008

I have a form with 10 button. I want to have one click event for all these buttons. However, until vb when you can have the handles, C# doesn't seem to have it.

VB: Private Sub NumberButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,

So when I click on button 0, 1, 2 etc I would like it to go into the event below.

Want is the method to do this in C#,

Many thanks,

Steve

Code:
private void NumberButton_Click(object sender, System.EventArgs e)
{
     txtDisplay =+ ((Button)sender).Text
}
 
The easiest way is to create the handler for one button using the property grid, again using the property grid set the handler for the other buttons to refer to the original handler.

Hope this makes sense.




[vampire][bat]
 
Do you mean += instead of =+?

Also, what property of txtDisplay are you trying to set?

Visual Studio should not have a problem pointing several buttons to the same click event. For the easy way, just select "NumberButton_Click" in each Button's click event from the form designer.

If you want to do it in code, take a look at FormX.Designer.cs to see how it is done.

OTTOMH something like
Code:
this.myButton.Click += new System.EventHandler(NumberButton_Click);

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Yeah it's a new feature in VS2008 ;-)

Or, Off The Top Of My Head.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
I should have known, I have VS2008! Lol, thanks. I deal with so many acronyms already, but have never seen that one
 
The other option is to highlite all the controls on the form, switch to the Action view and double click or add a name like buttons_clicked to the Click action.

All the painful work of wiring up is done for you.

 
Hello,

Thanks for all the replies. Problem solved. However, I could not find "action view".

Where is action view?

Many thanks,

Steve
 
click on the button in the form. Go to the properties window. Above the window you will see a lightning bolt. This is the "Action View" where it will show you all the available events.

 
Thanks,

Actually I have always referred to this as the events and not action.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top