Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So let's say I have 8 buttons in form.cs[Design] and once I click any of the buttons, it will call the same sql procedure. However. I don't know how to write that portion. I think I should put the loop inside this
C#
private void btn1_Click(object sender, EventArgs e)
       {

       }
. Could anyone pls suggest me on that? Thank you.

What I have tried:

C#
private void btn1_Click(object sender, EventArgs e)
        {

        }
Posted
Updated 11-Sep-22 19:38pm

To add to my answer that Rick linked to, inside the button event handler you can access the actual button that was clicked via the sender parameter:
C#
private void MyButton_Click(object sender, EventArgs e)
   {
   if (sender is Button btn)
      {
      Console.WriteLine(btn.Text);
      ...
      }
   }
"looping through" buttons doesn't make any real sense, as the user can only physically click on one of them at a time!
 
Share this answer
 
Comments
RickZeeland 12-Sep-22 2:10am    
5d! heh, heh :)
See answer on CodeProject here: One event for multiple buttons[^]
 
Share this answer
 

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