How to use radio button controls in WinForm?

To use a radio button control in WinForm, you can follow these steps:

  1. Create a new WinForm application project in Visual Studio.
  2. Drag a RadioButton control onto the form in the form designer.
  3. Set the properties of radio buttons, such as Text (displayed text), Name (control’s name), Checked (whether it is selected), etc.
  4. If you need multiple radio buttons, you can drag multiple radio button controls onto the form and configure their properties.
  5. In the code file of the form, you can handle the change in the state of a radio button using event handlers. For example, you can use the CheckedChanged event to detect if the radio button’s state has changed.

Here is a simple example code that demonstrates how to use a radio button control in WinForm.

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked)
    {
        MessageBox.Show("单选按钮1被选中");
    }
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton2.Checked)
    {
        MessageBox.Show("单选按钮2被选中");
    }
}

By following the steps and example code above, you can successfully use radio button controls in WinForm and customize and implement functionality as needed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *