简体   繁体   English

单选按钮选中时如何使文本框不可见

[英]How to make a textbox invisible when radio button checked

I am working on C# application. 我正在使用C#应用程序。 I have 10 radio buttons in a group panel, so now if I only checked the radioButton10 then textBox1 will be visible, if I checked someone of the other radio Buttons (radioButton1 .... radioButton9) then textBox1 should be invisible. 我在组面板中有10个单选按钮,所以现在如果我仅选中radioButton10,则textBox1将可见;如果我选中了其他单选按钮中的某个(radioButton1 .... radioButton9),则textBox1应该不可见。 I wrote the following code but the textBox1 is still visible. 我编写了以下代码,但textBox1仍然可见。 If the code is right where can I wrote it(form load, some function ... etc) if it's NOT, then Please Help. 如果代码正确,那么我在哪里可以写(表单加载,某些函数...等),如果不是,那么请帮忙。

    public TeamInfoForm()
    {
        InitializeComponent();
        showTeam();
        if (radioButton10 .Checked)
            textBox1 .Visible = true;
        else
            textBox1 .Visible = false;

    }

I think you forgot to implement the event that occur when you check or uncheck the radiobutton. 我认为您忘记了执行选中或取消选中单选按钮时发生的事件。 Try to imlpement "OnCheckChanged" event for the radiobutton and you have to set autopostback to true if you want that the event occurs, otherwise the event won't work. 尝试为单选按钮插入“ OnCheckChanged”事件,如果您希望该事件发生,则必须将autopostback设置为true,否则该事件将不起作用。

Initially you have to set the Visible property of textBox1 to false in Forms Designer. 最初,您必须在Forms Designer中将textBox1的Visible属性设置为false。 Otherwise you can set it in FormInitialize() method. 否则,可以在FormInitialize()方法中进行设置。 Next you have write code like below 接下来,您将编写如下代码

    public void ToggleTextBox()
    {
        textBox1.Visible = radioButton3.Checked;
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        ToggleTextBox();
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        ToggleTextBox();
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        ToggleTextBox();
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM