SoFunction
Updated on 2025-04-04

Methods for enumeration types and radiobox association operations in C#

This article describes the methods of enumerating types and radiobox association operations in C#. Share it for your reference. The specific analysis is as follows:

With enum we can list types, with radio boxes and check boxes we can select them with the mouse. However, when programming, I feel that writing code is more troublesome, so I want to automatically associate it. So I tried it and recorded it as follows.

If a week's enum:

public enum Week
{
  Week一 = 0,
  Week二,
  Week三,
  Week四,
  Week五,
  Week六,
  Week天
}

Associated with 7 RadioButtons, that is, radio boxes.
The first step defines Monday = 0 in enum;
The second step is defined as follows in the initialization function:

public MainForm()
{
 //
 // The InitializeComponent() call is required 
 //for Windows Forms designer support.
 //
 InitializeComponent();
 //
 // TODO: Add constructor code 
 // after the InitializeComponent() call.
 //
 int idx = 0;
 foreach(Control c in )
 {
  if(c is RadioButton)
  {
   ((RadioButton)c).Text = ((Week)idx).ToString();
   ((RadioButton)c).Tag = ((Week)idx);
   idx++;
  }
 }
}

Step 3 to add test code:

void Button1Click(object sender, EventArgs e)
{
 foreach(Control c in )
 {
  if(c is RadioButton)
  {
   if(((RadioButton)c).Checked == true)
   {
    Week week = (Week)(((RadioButton)c).Tag);
    (());
   }
  }
 }
}

Note: The order of controls in groupbox is controlled in these codes. If you find that the order is incorrect, you need to readjust it.

this.(this.radioButton1);
this.(this.radioButton2);
this.(this.radioButton3);
this.(this.radioButton4);
this.(this.radioButton5);
this.(this.radioButton6);
this.(this.radioButton7);

I hope this article will be helpful to everyone's C# programming.