【问题标题】:How do I make and if else statement for individual radio buttons?如何为单个单选按钮制作 if else 语句?
【发布时间】:2016-03-19 10:11:39
【问题描述】:

我有这个创建单选按钮的代码,当我点击一个按钮时,当单选按钮检查 If Else 语句时,有人可以告诉我分别选择每个按钮的代码吗?如何使每个单选按钮的文本不同?

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);

            }
        }

【问题讨论】:

    标签: c# winforms dynamic radio-button


    【解决方案1】:

    每个按钮的文本已经不同。当您通过 for 循环时,您将在末尾附加一个数字。听起来您只需要在动态创建的单选按钮的CheckedChanged 事件上添加一个处理程序,这样您就可以根据被点击的按钮做一些事情。

    您只需将此行添加到您的 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...
        }
        // ...
    }
    

    【讨论】:

    • 非常感谢 Jason 的帮助,我试过了,对我有用。
    • 出了点问题,当我从单选按钮 1 转到 2 时,它会再次触发单选按钮 1,然后再触发 2。
    • @dominique1256 这是因为当一个被选中时,另一个未被选中。您可能需要添加一个条件来确保发送者对象是选中的对象。
    【解决方案2】:

    根据您的 cmets,我建议使用常规的 Button 而不是 RadioButton

    这是一个为每个消息显示唯一消息的实现:

    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");
    }
    

    【讨论】:

    • 出了点问题,当我从单选按钮 1 转到 2 时,它会再次触发单选按钮 1,然后再触发 2。- Dominique1256 刚刚编辑
    • 通过将所有 RadioButtons 放在同一个面板中,它们会自动组合在一起。因为它们被组合在一起,所以一次只允许“检查”其中一个。因此,它取消检查先前检查的最后一个并为两者触发 CheckChanged 事件。如果您查看我的代码,它会检查“Checked”属性,并让您决定要写什么作为文本。如果您想允许同时检查多个,那么您应该使用 CheckBoxes 而不是 RadioButtons。
    • @Don-它也不适合我。对于杰森的解决方案,当我在单选按钮 1 之后检查单选按钮 2 时。当我单击确定时,单选按钮 1 再次触发一个消息框,然后单选按钮 2 的消息框触发。
    • 我想我明白了,我想要的是当我在单选按钮 1 之后检查单选按钮 2 时,我不想看到单选按钮 1 再次触发。不使用 Panel 的解决方案是什么?
    • CHECKING Button 2 将自动 UNCHECK Button 1 并且 CheckCHANGED 事件将为两者触发。您想允许检查多个 - 还是一次只检查一个?或者您只想更改选中的文本?这取决于你想要什么。您可以尝试仅使用 CLICK 事件进行注册。更改 b.CheckedChanged += b_CheckedChanged; TO b.Click += b_CheckedChanged;或者只处理 if (b.Checked == true) 并将代码从 ELSE 部分中取出。
    猜你喜欢
    • 2014-05-31
    • 2015-09-13
    • 2023-03-12
    • 2016-03-19
    • 2013-07-21
    • 1970-01-01
    • 2017-12-09
    • 2014-12-20
    • 2013-08-03
    相关资源
    最近更新 更多