Introduction
In this post, I will demonstrate how to use a single function to trap all clickable buttons.
Events in .NET are very different from old events like Visual Basic 6.0, if you remember.
When I first migrated to OOP C#, I was very excited to use this feature to trap all buttons in one click.
THIS IS THE FORM SAMPLE
We have 10 buttons on it:

Here is the complete code:
- using System;
- using System.Windows.Forms;
- namespace MultiplesButtonClick2SingleFunction
- {
- public partial class FormRegular1 : Form
- {
- public FormRegular1()
- =>
- InitializeComponent();
- private void Form1_Load(object sender, EventArgs e)
- {
- // Read all controls in the form
- foreach(Control ctl in Controls)
- if (ctl is Button bt) // Select only Button type
- bt.Click += ClickEvents; // Add the event
- }
- private void ClickEvents(object sender, EventArgs e)
- {
- // Check if is a Button and init bt variable
- if (!(sender is Button bt)) return;
- // Check by the text caption of the button
- switch(bt.Text)
- {
- case "button1":
- {
- new FormAnonymous1().Show();
- // Do something
- break;
- }
- case "button2":
- {
- // Do something
- break;
- }
- case "button3":
- {
- // Do something
- break;
- }
- case "button4":
- {
- // Do something
- break;
- }
- case "button5":
- {
- // Do something
- break;
- }
- case "button6":
- {
- // Do something
- break;
- }
- case "button7":
- {
- // Do something
- break;
- }
- case "button8":
- {
- // Do something
- break;
- }
- case "button9":
- {
- // Do something
- break;
- }
- case "button10":
- {
- // Do something
- break;
- }
- }
- }
- }
- }
Plus - Using Anonymous
Using anonymous is a bit different, but I designed the code more efficiently in the following sample:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MultiplesButtonClick2SingleFunction
- {
- public partial class FormAnonymous1 : Form
- {
- public FormAnonymous1()
- =>
- InitializeComponent();
- private void Form1_Load(object sender, EventArgs e)
- {
- foreach (Control ctl in Controls)
- // Check Button type
- if (ctl is Button bt)
- // Create anonymous funcions
- bt.Click += (ss, ee) =>
- // Pass only the text
- { ClickEvents(((Button)ss).Text); };
- }
- /// <summary>
- /// Check only the text
- /// </summary>
- /// <param name="text"></param>
- private void ClickEvents(string text)
- {
- switch( text)
- {
- case "button1":
- {
- new FormRegular1().Show();
- // Do something
- break;
- }
- case "button2":
- {
- // Do something
- break;
- }
- case "button3":
- {
- // Do something
- break;
- }
- case "button4":
- {
- // Do something
- break;
- }
- case "button5":
- {
- // Do something
- break;
- }
- case "button6":
- {
- // Do something
- break;
- }
- case "button7":
- {
- // Do something
- break;
- }
- case "button8":
- {
- // Do something
- break;
- }
- case "button9":
- {
- // Do something
- break;
- }
- case "button10":
- {
- // Do something
- break;
- }
- }
- }
- }
- }
PLUS 2 - MIX MULTIPLES TYPE CONTROLS
In this sample, the Click event is trapped together.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MultiplesButtonClick2SingleFunction
- {
- public partial class FormAnonymous1 : Form
- {
- public FormAnonymous1()
- =>
- InitializeComponent();
- private void Form1_Load(object sender, EventArgs e)
- {
- foreach (Control ctl in Controls)
- // Check Button type
- if (ctl is Button bt)
- // Create anonymous funcions
- bt.Click += (ss, ee) =>
- // Pass only the text
- { ClickEvents(((Button)ss).Text); };
- else if (ctl is CheckBox)
- ctl.Click += (ss, ee) =>
- // Pass only the text
- { ClickEvents(((CheckBox)ss).Text); };
- }
- /// <summary>
- /// Check only the text
- /// </summary>
- /// <param name="text"></param>
- private void ClickEvents(string text)
- {
- switch( text)
- {
- case "checkBox1":
- {
- // Do something
- break;
- }
- case "button1":
- {
- new FormRegular1().Show();
- // Do something
- break;
- }
- case "button2":
- {
- // Do something
- break;
- }
- case "button3":
- {
- // Do something
- break;
- }
- case "button4":
- {
- // Do something
- break;
- }
- case "button5":
- {
- // Do something
- break;
- }
- case "button6":
- {
- // Do something
- break;
- }
- case "button7":
- {
- // Do something
- break;
- }
- case "button8":
- {
- // Do something
- break;
- }
- case "button9":
- {
- // Do something
- break;
- }
- case "button10":
- {
- // Do something
- break;
- }
- }
- }
- }
- }
Conclusion
Putting all click events in a single place is good way to improve the programming.
Happy coding.

Sourav Kumar DasPosted Nov 3, 2019, 10:40 PM
Nice useful Article Sir.