C# 如何对单个单选按钮进行if else语句?

C# 如何对单个单选按钮进行if else语句?,c#,winforms,dynamic,radio-button,C#,Winforms,Dynamic,Radio Button,我有一个创建单选按钮的代码,有人能告诉我当单选按钮被选中时分别选择每个按钮的代码吗?。如何使每个单选按钮的文本不同 private void createButtons() { flowLayoutPanel1.Controls.Clear(); for(int i = 0;i <10;i++) { RadioButton b = new RadioButton();

我有一个创建单选按钮的代码,有人能告诉我当单选按钮被选中时分别选择每个按钮的代码吗?。如何使每个单选按钮的文本不同

private void createButtons()
        {
            flowLayoutPanel1.Controls.Clear();
            for(int i = 0;i <10;i++)
            {
                RadioButton b = new RadioButton();
                b.Name = i.ToString();
                b.Text = "radiobutton" + i.ToString();
                b.AutoSize = true;
                flowLayoutPanel1.Controls.Add(b);

            }
        }
private void createButtons()
{
flowLayoutPanel1.Controls.Clear();

for(int i=0;i每个按钮的文本已经不同。当您在
for
循环中移动时,您在末尾添加了一个数字。听起来您只需要在动态创建的单选按钮事件上添加一个处理程序,这样您就可以根据单击的单选按钮执行某些操作

您只需将此行添加到
for
循环中的构建步骤:

b.CheckedChanged += RadioButtonClicked;
然后定义适当的方法:

private void RadioButtonClicked(object sender, EventArgs e)
{
    var radioButton = (RadioButton)sender;

    // Only run on checked items (per your comments).
    // This condition will cause the uncheck action/event to exit here.
    if (!radioButton.Checked)
    {
        return;
    }

    // Alternately, you could use a switch statement.
    if (radioButton.Name == "1")
    {
        // do something...
    }
    else if (radioButton.Name == "2")
    {
        // do something else...
    }
    // ...
}

根据您的意见,我建议使用常规的
按钮
,而不是
单选按钮

以下是一个实现,用于为每个应用程序显示唯一的消息:

private void createButtons()
{
    flowLayoutPanel1.Controls.Clear();
    for (int i = 1; i <= 65; i++)
    {
        Button b = new Button();
        b.Name = i.ToString();
        b.Text = "Translate" + i.ToString();
        b.Click += b_Click;
        flowLayoutPanel1.Controls.Add(b);
    }
}

void b_Click(object sender, EventArgs e)
{
    Button b = sender as Button;

    string TextBoxValue = string.Empty;

    switch (b.Name)
    {
        case "1":
            TextBoxValue = "Get the Translation for item # 1 here";
            break;
        case "2":
            TextBoxValue = "Get the Translation for item # 2 here";
            break;
        // ETC....  3,4,5
        default:
            TextBoxValue = "Button #(" + b.Name + ") is not handled";
            break;
    }

    MessageBox.Show(this, TextBoxValue, "Translation");
}
private void createButtons()
{
flowLayoutPanel1.Controls.Clear();

对于(int i=1;非常感谢您的帮助Jason,我试过了,它对我有效。出了点问题,当我从单选按钮1转到2时,它会一次又一次地触发单选按钮1,然后再触发2。@dominique1256这是因为当一个被选中时,另一个被选中。您可能需要添加一个条件,以确保发送方对象是选中的对象。有些事情是错误的,当我从单选按钮1到2时,它会再次触发单选按钮1,然后触发2。-Dominique1256刚才编辑将所有单选按钮放在同一面板中,它们会自动分组在一起。因为它们被分组在一起,所以只允许“检查”其中一个一次。因此,它取消检查以前检查过的最后一个,并为这两个事件触发CheckChanged事件。如果查看我的代码,它会检查“已检查”事件属性并让您决定将什么作为文本写入。如果您希望允许同时选中多个,则应使用复选框而不是单选按钮。@Don这对我也不起作用。对于Jason的解决方案,当我选中单选按钮1后的单选按钮2。单选按钮1。当我单击“确定”时,会再次触发消息框和单选按钮1然后radion button2的messagebox启动。我想我明白了,我想要的是当我在radio button1之后选中radio button2时,我不想看到radio button1再次启动。不使用面板的解决方案是吗?选中按钮2将自动取消选中按钮1,并且CheckCHANGED事件将同时为这两个按钮启动。是否允许选中多个按钮d-或者一次只更改一个?或者您只想更改选中的文本?这取决于您想要什么。您可以尝试仅注册CLICK事件。更改b.CheckedChanged+=b_CheckedChanged;更改为b.CLICK+=b_CheckedChanged;或者只处理if(b.checked==true)并将代码从ELSE部分中取出。