C# 我们如何取消选中单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10698239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:52:31  来源:igfitidea点击:

How can we uncheck a radio button

c#.net

提问by Running Rabbit

I want to know how can we uncheck a radio button. It should work like a checkbox.

我想知道我们如何取消选中单选按钮。它应该像复选框一样工作。

回答by npinti

Radio buttons are used when you want the user to choose, usually, one or more items from a series of options, so by the end, you will have at least one selected item. If you want to provide the user to uncheck, then, you should really be using a Checkbox in the first place.

当您希望用户从一系列选项中选择一个或多个项目时,通常会使用单选按钮,因此到最后,您将至少选择一个项目。如果您想让用户取消选中,那么您首先应该真正使用 Checkbox。

At most in your case, you could provide some functionality such as a button to resetthe radio buttons, by doing something like rdBtn.Checked = false;

在您的情况下,您最多可以提供一些功能,例如通过执行以下操作来重置单选按钮的按钮rdBtn.Checked = false;

回答by Milan Halada

This might work:

这可能有效:

private void radio_click(object sender, EventArgs e)
{
  if (radio.Checked)
  {
    radio.Checked = false;
  }
}

回答by Shiv Sahu

Use the following code to use radio button like check box.

使用以下代码使用单选按钮,如复选框。

    bool isChecked =false;
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        isChecked = radioButton1.Checked;
    }

    private void radioButton1_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked && !isChecked)
            radioButton1.Checked = false;
        else
        {
            radioButton1.Checked = true;
            isChecked = false;
        }
    }