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

Conclusion

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