如何在单击文本框时清除单选按钮

时间:2021-07-29 21:16:10

am just wondering is there a way for me to make it so that when a textbox is clicked it will clear the radio button. also to note I have group the radio buttons. here is the code obviously this is not all of the code but it only this part i need help with thanks.

我只是想知道有没有办法让我这样做,以便在点击文本框时它将清除单选按钮。另外要注意我已将收音机按钮分组。这里的代码显然不是所有的代码,但只有这部分我需要帮助谢谢。

here's an image of the program

这是该计划的图像

如何在单击文本框时清除单选按钮

if (newMaterialother.Length < 1)                      
throw new Exception("must be more than 1 character long ");                
  if (rbnProcelain.IsChecked == false)                    
 newMaterialother = "Procelain";                
 if (rbnChina.IsChecked == false)                    
 newMaterialother = "Chain";                
 if (rbnClay.IsChecked == false)                    
 newMaterialother = "Clay";     

1 个解决方案

#1


0  

If we assume the group box is grpMaterialOptions and the text box is txtOther, then if you are not going to be adding more radio buttons, you can just set the 'Checked' property on all of the radio buttons to 'false' when clicking the text box.

如果我们假设组框是grpMaterialOptions并且文本框是txtOther,那么如果你不打算添加更多的单选按钮,你可以在单击时将所有单选按钮上的'Checked'属性设置为'false'。文本框。

If we also assume the form is called Form1, then either add a Click event against txtOther from within the Form's constructor (or just from in the designer):

如果我们还假设表单被称为Form1,那么要么从Form的构造函数(或者只是从设计器中)添加一个针对txtOther的Click事件:

        public Form1()
    {
        InitializeComponent();
        txtOther.Click += txtOther_Click;
    }

Then, un-check each of the radio buttons within the click event:

然后,取消选中click事件中的每个单选按钮:

        private void txtOther_Click(object sender, EventArgs e)
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
    }

Alternatively, if you're planning on adding more radio buttons to the panel or you're planning on just having a large number of radio buttons, you might try something more verbose; the following will only need implemented once and will work for however many radio buttons you want to add in:

或者,如果您计划在面板上添加更多单选按钮,或者您计划只使用大量单选按钮,则可能会尝试更详细的内容;以下只需要实现一次,并且可以用于您想添加的许多单选按钮:

        private void txtOther_Click(object sender, EventArgs e)
    {
        foreach (Control c in grpMaterialOptions.Controls)
        {
            if (c is RadioButton)
            {
                (c as RadioButton).Checked = false;
            }
        }
    }

#1


0  

If we assume the group box is grpMaterialOptions and the text box is txtOther, then if you are not going to be adding more radio buttons, you can just set the 'Checked' property on all of the radio buttons to 'false' when clicking the text box.

如果我们假设组框是grpMaterialOptions并且文本框是txtOther,那么如果你不打算添加更多的单选按钮,你可以在单击时将所有单选按钮上的'Checked'属性设置为'false'。文本框。

If we also assume the form is called Form1, then either add a Click event against txtOther from within the Form's constructor (or just from in the designer):

如果我们还假设表单被称为Form1,那么要么从Form的构造函数(或者只是从设计器中)添加一个针对txtOther的Click事件:

        public Form1()
    {
        InitializeComponent();
        txtOther.Click += txtOther_Click;
    }

Then, un-check each of the radio buttons within the click event:

然后,取消选中click事件中的每个单选按钮:

        private void txtOther_Click(object sender, EventArgs e)
    {
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
    }

Alternatively, if you're planning on adding more radio buttons to the panel or you're planning on just having a large number of radio buttons, you might try something more verbose; the following will only need implemented once and will work for however many radio buttons you want to add in:

或者,如果您计划在面板上添加更多单选按钮,或者您计划只使用大量单选按钮,则可能会尝试更详细的内容;以下只需要实现一次,并且可以用于您想添加的许多单选按钮:

        private void txtOther_Click(object sender, EventArgs e)
    {
        foreach (Control c in grpMaterialOptions.Controls)
        {
            if (c is RadioButton)
            {
                (c as RadioButton).Checked = false;
            }
        }
    }