简体   繁体   English

C#如何取消选中动态创建的单选按钮?

[英]C# how can I uncheck dynamically created radio buttons?

I'm dynamically creating some radio buttons in a form, and I would like users to have the ability to deselect them. 我正在动态创建表单中的一些单选按钮,我希望用户能够取消选择它们。 Users' options should be select one, or select none(safer to use radio buttons than checkboxes). 用户的选项应该选择一个,或者不选择(使用安全性比单选框更安全)。 I tried using the EventHandler and creating an event, but I'm not having much luck getting it to work properly. 我尝试使用EventHandler并创建一个事件,但运气不佳,使其无法正常工作。

Here's what I've got making the buttons: 这是我要做的按钮:

private void makeRadioButtons(string size, Panel pnl)
    {
        for (int i = 0; i < 2; i++)
        {
            RadioButton btn = new RadioButton();
            btn.AutoSize = true;
            btn.Name = i;
            btn.Text = i;
            btn.Location = new Point(100*i, 0);
            btn.Click += new EventHandler(radioHandler);
            pnl.Controls.Add(btn);
        }
}

And here is my event handler: 这是我的事件处理程序:

private void radioHandler(object sender, EventArgs e)
    {
        RadioButton rdo = sender as RadioButton;
        if(rdo.Checked)
        {
            rdo.Checked = false;
        }
        else
        {
            rdo.Checked = true;
        }
    }

When I compile I get the buttons, but can't select or deselect them. 编译时会得到按钮,但是无法选择或取消选择它们。 I feel like I'm close, but I just can't figure out what I'm doing wrong here. 我感觉自己已经接近了,但是我无法弄清楚我在做什么错。

RadioButtons are checked automatically by default. 默认情况下会自动检查RadioButtons。 You can avoid that by setting their AutoCheck property to false when you create them. 您可以通过在创建它们时将其AutoCheck属性设置为false来避免这种情况。

Then you need to change your handler like this: 然后,您需要像这样更改处理程序:

private void radioHandler(object sender, System.EventArgs e)
{
    RadioButton rbt = (RadioButton)sender;
    if (rbt.Checked)
    {
        rbt.Checked = false;
        return;
    }

    rbt.Checked = true;
    foreach (RadioButton r in pnl.Controls.OfType<RadioButton>())
        if (r != rbt) r.Checked = false;
}

So if the clicked button was checked, it is unchecked. 因此,如果单击的按钮被选中,则未选中。 If it was not checked, we check it and uncheck all the others (this is necessary since we set AutoCheck to false ). 如果未选中,则将其选中,然后取消选中所有其他复选框(这是必需的,因为我们将AutoCheck设置为false )。

It s your event handler code that makes the mess. 麻烦的是您的事件处理程序代码。

Radiobuttons are automatically checked/unchecked when user clicks. 用户单击时,单选按钮会自动选中/取消选中。

Your code makes the opposite ! 您的代码则相反!

Edit 编辑

Just removing of the handler is needed. 只需要删除处理程序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#的“列”中格式化动态创建的单选按钮 - Format dynamically created Radio buttons in Columns in c# 如果c#中包含一些按钮,如何保存动态创建的用户控件? - How can I save a dynamically created user control if it contains some buttons in c#? 如何清除,取消选择或取消选中动态单选按钮列表?(ASP.NET,C#) - How Clear, Deselect or Uncheck Dynamic Radio Buttons List?(ASP.NET, C#) 如何绑定到WPF中动态创建的单选按钮? - How to bind to radio buttons created dynamically in WPF? 如何在c#中将CSS添加到动态创建的div中? - How can i add css to a dynamically created div in c#? 在asp.net中的C#代码中的GridView中设置动态创建的单选按钮的组名 - Set the groupname of dynamically created radio buttons in gridview in C# code in asp.net 关于c#中动态创建的单选按钮 - About dynamically created radio button in c# 如何正确地将事件分配给 c# Winforms 中动态创建的按钮? - How to properly assign events to dynamically created buttons in c# Winforms? 动态创建按钮时,如何在if else语句上调用单个单选按钮? - How Do I call individual radio buttons on if else statement when buttons are created dynamically? 使用按钮和requirefieldvalidators动态创建表c# - Dynamically created table with buttons and requirefieldvalidators c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM