C#-单选按钮检查更改事件选中时不响应

C#-单选按钮检查更改事件选中时不响应,c#,winforms,radio-button,C#,Winforms,Radio Button,请注意,我有一个单选按钮,它是我从列表中动态添加的(在数据库读取期间获得的),它显示良好,但不会触发检查更改事件 public void fillradioButton() { for (int i = 0; i < officeLists.Count; i++) { RadioButton rbutton = new RadioButton() { Name = "rbutton", Text

请注意,我有一个单选按钮,它是我从列表中动态添加的(在数据库读取期间获得的),它显示良好,但不会触发检查更改事件

public void fillradioButton()
{
    for (int i = 0; i < officeLists.Count; i++)
    {
        RadioButton rbutton = new RadioButton()
        {
            Name = "rbutton",
            Text = candidate_surname + " " + candidate_name,
            Left = _RadiobuttonPos.X,
            Top = _RadiobuttonPos.Y,
        };

        this.Controls.Add(rbutton);
        _RadiobuttonPos.Y += 25;
    }
}

private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
    MessageBox.Show("Okay I see U");                
}
public void fillradioButton()
{
for(int i=0;i
如果选中任何单选按钮,请显示消息框。
谢谢。

您需要将单击处理程序添加到单选按钮上的事件中。在初始化单选按钮后,在循环内部的某个点执行以下操作:

rbutton.CheckedChanged += new EventHandler(RadioButtonCheckedChanged);


由于您正在动态创建按钮

将事件添加到单选按钮,例如:

 public void fillradioButton()
{
    for (int i = 0; i < officeLists.Count; i++)
    {
        RadioButton rbutton = new RadioButton()
        {
            Name = "rbutton",
            Text = candidate_surname + " " + candidate_name,
            Left = _RadiobuttonPos.X,
            Top = _RadiobuttonPos.Y,
        };

        If(i == 0) rbutton.CheckedChanged += RadioButtonCheckedChanged;

        this.Controls.Add(rbutton);
        _RadiobuttonPos.Y += 25;
    }
}
public void fillradioButton()
{
for(int i=0;i
另一个问题是
MessageBox.Show(“好的,我看到你了”)重复两次,选中第一个选项时重复一次。我需要它只显示一次,与所选选项无关。谢谢。尝试在for循环之外添加类似这样的事件:this.Controls[“rbutton”].CheckedChanged+=RadioButtonCheckedChanged;发生了什么?实际上,现在就用if试试,它是更干净的解决方案谢谢,现在我需要让用户能够选择单选按钮一次,即在单击单选按钮后,它将被禁用。这与此问题不再相关。无论如何,添加类似rbutton.disabled=true的内容;在显示MessageBox感谢您的帮助后的CheckedChanged事件中,
rbutton.CheckedChanged+=RadioButtonCheckedChanged帮助。但选中第二个选项时,messagebox会重复两次,选中第一个选项时,messagebox会重复一次。我需要它只显示一次,与所选选项无关。非常感谢。
 public void fillradioButton()
{
    for (int i = 0; i < officeLists.Count; i++)
    {
        RadioButton rbutton = new RadioButton()
        {
            Name = "rbutton",
            Text = candidate_surname + " " + candidate_name,
            Left = _RadiobuttonPos.X,
            Top = _RadiobuttonPos.Y,
        };

        If(i == 0) rbutton.CheckedChanged += RadioButtonCheckedChanged;

        this.Controls.Add(rbutton);
        _RadiobuttonPos.Y += 25;
    }
}