Multiples Click Events In A Single Function/Place

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:
  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace MultiplesButtonClick2SingleFunction  
  5. {  
  6.     public partial class FormRegular1 : Form  
  7.     {  
  8.         public FormRegular1()  
  9.         =>  
  10.             InitializeComponent();          
  11.   
  12.         private void Form1_Load(object sender, EventArgs e)  
  13.         {  
  14.             // Read all controls in the form  
  15.             foreach(Control ctl in Controls)              
  16.                 if (ctl is Button bt) // Select only Button type  
  17.                         bt.Click += ClickEvents;   // Add the event           
  18.         }  
  19.   
  20.         private void ClickEvents(object sender, EventArgs e)  
  21.         {  
  22.             // Check if is a Button and init bt variable  
  23.             if (!(sender is Button bt)) return;  
  24.   
  25.             // Check by the text caption of the button  
  26.             switch(bt.Text)  
  27.             {  
  28.                 case "button1":  
  29.                     {  
  30.                        new FormAnonymous1().Show();  
  31.                         // Do something  
  32.                         break;  
  33.                     }  
  34.                 case "button2":  
  35.                     {  
  36.                         // Do something  
  37.                         break;  
  38.                     }  
  39.                 case "button3":  
  40.                     {  
  41.                         // Do something  
  42.                         break;  
  43.                     }  
  44.                 case "button4":  
  45.                     {  
  46.                         // Do something  
  47.                         break;  
  48.                     }  
  49.                 case "button5":  
  50.                     {  
  51.                         // Do something  
  52.                         break;  
  53.                     }  
  54.                 case "button6":  
  55.                     {  
  56.                         // Do something  
  57.                         break;  
  58.                     }  
  59.                 case "button7":  
  60.                     {  
  61.                         // Do something  
  62.                         break;  
  63.                     }  
  64.                 case "button8":  
  65.                     {  
  66.                         // Do something  
  67.                         break;  
  68.                     }  
  69.                 case "button9":  
  70.                     {  
  71.                         // Do something  
  72.                         break;  
  73.                     }  
  74.                 case "button10":  
  75.                     {  
  76.                         // Do something  
  77.                         break;  
  78.                     }  
  79.             }  
  80.         }  
  81.   
  82.            
  83.     }  
  84. }  
Plus - Using Anonymous
 
Using anonymous is a bit different, but I designed the code more efficiently in the following sample:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MultiplesButtonClick2SingleFunction  
  12. {  
  13.     public partial class FormAnonymous1 : Form  
  14.     {  
  15.         public FormAnonymous1()  
  16.         =>  
  17.             InitializeComponent();          
  18.   
  19.         private void Form1_Load(object sender, EventArgs e)  
  20.         {  
  21.             foreach (Control ctl in Controls)  
  22.                 // Check Button type  
  23.                 if (ctl is Button bt)  
  24.                     // Create anonymous funcions  
  25.                     bt.Click += (ss, ee) =>  
  26.                     // Pass only the text  
  27.                     { ClickEvents(((Button)ss).Text); };              
  28.         }  
  29.   
  30.         /// <summary>  
  31.         /// Check only the text  
  32.         /// </summary>  
  33.         /// <param name="text"></param>  
  34.         private void ClickEvents(string text)  
  35.         {         
  36.             switch( text)  
  37.             {  
  38.                 case "button1":  
  39.                     {  
  40.                         new FormRegular1().Show();  
  41.                         // Do something  
  42.                         break;  
  43.                     }  
  44.                 case "button2":  
  45.                     {  
  46.                         // Do something  
  47.                         break;  
  48.                     }  
  49.                 case "button3":  
  50.                     {  
  51.                         // Do something  
  52.                         break;  
  53.                     }  
  54.                 case "button4":  
  55.                     {  
  56.                         // Do something  
  57.                         break;  
  58.                     }  
  59.                 case "button5":  
  60.                     {  
  61.                         // Do something  
  62.                         break;  
  63.                     }  
  64.                 case "button6":  
  65.                     {  
  66.                         // Do something  
  67.                         break;  
  68.                     }  
  69.                 case "button7":  
  70.                     {  
  71.                         // Do something  
  72.                         break;  
  73.                     }  
  74.                 case "button8":  
  75.                     {  
  76.                         // Do something  
  77.                         break;  
  78.                     }  
  79.                 case "button9":  
  80.                     {  
  81.                         // Do something  
  82.                         break;  
  83.                     }  
  84.                 case "button10":  
  85.                     {  
  86.                         // Do something  
  87.                         break;  
  88.                     }  
  89.             }  
  90.         }  
  91.     }  
  92. }  
PLUS 2 - MIX MULTIPLES TYPE CONTROLS
 
In this sample, the Click event is trapped together.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MultiplesButtonClick2SingleFunction  
  12. {  
  13.     public partial class FormAnonymous1 : Form  
  14.     {  
  15.         public FormAnonymous1()  
  16.         =>  
  17.             InitializeComponent();          
  18.   
  19.         private void Form1_Load(object sender, EventArgs e)  
  20.         {  
  21.             foreach (Control ctl in Controls)  
  22.                 // Check Button type  
  23.                 if (ctl is Button bt)  
  24.                     // Create anonymous funcions  
  25.                     bt.Click += (ss, ee) =>  
  26.                     // Pass only the text  
  27.                     { ClickEvents(((Button)ss).Text); };       
  28.             else if (ctl is CheckBox)  
  29.                     ctl.Click += (ss, ee) =>  
  30.                     // Pass only the text  
  31.                     { ClickEvents(((CheckBox)ss).Text); };  
  32.   
  33.         }  
  34.   
  35.         /// <summary>  
  36.         /// Check only the text  
  37.         /// </summary>  
  38.         /// <param name="text"></param>  
  39.         private void ClickEvents(string text)  
  40.         {         
  41.             switch( text)  
  42.             {  
  43.                 case "checkBox1":  
  44.                     {  
  45.                         // Do something  
  46.                         break;  
  47.                     }  
  48.                 case "button1":  
  49.                     {  
  50.                         new FormRegular1().Show();  
  51.                         // Do something  
  52.                         break;  
  53.                     }  
  54.                 case "button2":  
  55.                     {  
  56.                         // Do something  
  57.                         break;  
  58.                     }  
  59.                 case "button3":  
  60.                     {  
  61.                         // Do something  
  62.                         break;  
  63.                     }  
  64.                 case "button4":  
  65.                     {  
  66.                         // Do something  
  67.                         break;  
  68.                     }  
  69.                 case "button5":  
  70.                     {  
  71.                         // Do something  
  72.                         break;  
  73.                     }  
  74.                 case "button6":  
  75.                     {  
  76.                         // Do something  
  77.                         break;  
  78.                     }  
  79.                 case "button7":  
  80.                     {  
  81.                         // Do something  
  82.                         break;  
  83.                     }  
  84.                 case "button8":  
  85.                     {  
  86.                         // Do something  
  87.                         break;  
  88.                     }  
  89.                 case "button9":  
  90.                     {  
  91.                         // Do something  
  92.                         break;  
  93.                     }  
  94.                 case "button10":  
  95.                     {  
  96.                         // Do something  
  97.                         break;  
  98.                     }  
  99.             }  
  100.         }  
  101.     }  
  102. }  

Conclusion

Putting all click events in a single place is good way to improve the programming.
 
Happy coding.