Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I make one event for multiple buttons?
I have a custom Russian keyboard with 33BigLetters and 33SmallLetters
(total=66Letters and also 66 buttons for each letter)
I know I did it some time ago, but right now I cant find anywhere and I don't remember how to do it.

I want to create a single event, that for each button pressed, a textbox.text add the button.text value.

its goes like this but I cant really do anything useful with this:
void MultiBtn_Click(Object sender,
                           EventArgs e)

            Button clickedButton = (Button)sender;
            textBox1.Text += clickedButton.Text;
Updated 31-Mar-23 20:45pm
[no name] 24-Nov-11 9:51am    
What more do you want? Clarify what you are trying to accomplish.

The code you have is pretty much what you need, all you need to do is add that to the Click event of each button. You can do that either in the designer via the Click drop down, or in code:
myButton1.Click += new EventHandler(MultiBtn_Click);
myButton2.Click += new EventHandler(MultiBtn_Click);
If it makes it easier though, there is also the Tag property which you could store the character in, if the Text property gives you problems.
Share this answer
_Q12_ 24-Nov-11 10:07am    
for each button from all 66 to make EventHandlers ?
I dont want to do that... I want ONE EventHandler for ALL 66 buttons.
[no name] 24-Nov-11 10:16am    
There is only ONE EventHandler. You need to assign it to all 66 buttons, there is no other way. Though you could try something like this

foreach(Control ctrl in Controls)
if(ctrl is Button)
ctrl.Click += new EventHandler(MultiBtn_Click);
_Q12_ 24-Nov-11 10:38am    
That's What I was looking for, Mark.
Many thanks.
OriginalGriff 24-Nov-11 10:31am    
As Mark said that is a single event handler - you have to hook them in somehow!
LanFanNinja 24-Nov-11 11:19am    
All you have to do is set all of the Click event handlers to use that event handler. Inside the handler, do this:

Button button = sender as Button;
switch (button.Name)
    case "button1":
        // do something for button1
    case "button2":
        // do something for button2
    // and so on


EDIT ====================================

Why was this 1-voted? it's a viable solution to the posted question.
Share this answer
_Q12_ 24-Nov-11 10:13am    
I am sure that its a much simpler way...
Error 1 Control cannot fall through from one case label ('case "button1":') to another,for this part:
case "button1": textBox1.Text += button1.Text;
#realJSOP 25-Nov-11 9:06am    
I don't understand what you're on about. The code I gave you will compile and run.

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