简体   繁体   中英

Enabling And Disabling Textboxes through Radiobuttons in C#

i have this form "Emp_Employee" where i have 4 radiobuttons: radio_ID, radio_ ... and 4 text boxes: id,name,salary,desgination when i load my form all the textboxes are disabled initially (which is fine) but when i check any radiobutton to enable it's relative textbox it arnt happening.. all textboxes remains disabled till the end no matter which radiobox is checked! Kindly Help..

private void Emp_Employee_Load(object sender, EventArgs e)
{
    id.Enabled = false;
    name.Enabled = false;
    salary.Enabled = false;
    designation.Enabled = false;

    if (radio_ID.Checked == true)
    {
        id.Enabled = true;
        name.Enabled = false;
        salary.Enabled = false;
        designation.Enabled = false;
    }
    else if (radio_Name.Checked == true)
    {
        name.Enabled = true;
        id.Enabled = false;
        salary.Enabled = false;
        designation.Enabled = false;
    }
    else if (radio_Salary.Checked == true)
    {
        salary.Enabled = true;
        id.Enabled = false;
        name.Enabled = false;
        designation.Enabled = false;
    }
    else if (radio_Designation.Checked == true)
    {
        designation.Enabled = true;
        id.Enabled = false;
        name.Enabled = false;
        salary.Enabled = false;
    }
}

Subscribe all radiobuttons to same CheckedChanged event handler and then just assign radiobutton's Checked state to Enabled state of textboxes:

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    id.Enabled = radio_ID.Checked;
    name.Enabled = radio_Name.Checked;
    salary.Enabled = radio_Salary.Checked;
    designation.Enabled = radio_Designation.Checked;
}

Another cool option is usage of databinding. You can simply bind these two properties on form load:

private void Emp_Employee_Load(object sender, EventArgs e)
{
    id.DataBindings.Add("Enabled", radio_ID, "Checked");
    name.DataBindings.Add("Enabled", radio_Name, "Checked");
    salary.DataBindings.Add("Enabled", radio_Salary, "Checked");
    designation.DataBindings.Add("Enabled", radio_Designation, "Checked");
}

Thus you even don't need to handle radiobutton's CheckedChanged event - textboxes will reflect state of radiobuttons automatically.

Page_Load event wont trigger the desired results.

You need to add another event in order to enable the textbox data. For example:

private void radioButton1_CheckedChanged(Object sender, 
                                         EventArgs e)
{
      salary.Enabled = true;
      id.Enabled = false;
      name.Enabled = false;
      designation.Enabled = false;
}

Refer this link.

Hope this helps

Try your text box enabling and disabling in radio button Checked changed event

RadioButton rb = new RadioButton();
rb.CheckedChanged += new EventHandler(rb_CheckedChanged);


void rb_CheckedChanged(object sender, EventArgs e)
    {
        //you do your text box enabling and disableing operation here
    }

I assume you're using WinForms, not Wpf

Your entire code is residing inside the Form.Load event which is executing only once per Form load. you need to move the update code to a code that is executing when each RadioButton is checked. Usually a RadioButton.CheckedChanged event.

if you use xaml , No need to code C#

Try this

    <RadioButton x:Name="radioButton0" GroupName="RadioButtonGroup1"/>
    <RadioButton x:Name="radioButton1" GroupName="RadioButtonGroup1"/>
    <RadioButton x:Name="radioButton2" GroupName="RadioButtonGroup1"/>
    <RadioButton x:Name="radioButton3" GroupName="RadioButtonGroup1"/>
    <TextBox x:Name="textBox0" IsEnabled="{Binding IsChecked, ElementName=radioButton0}"/>
    <TextBox x:Name="textBox1" IsEnabled="{Binding IsChecked, ElementName=radioButton1}"/>
    <TextBox x:Name="textBox2" IsEnabled="{Binding IsChecked, ElementName=radioButton2}"/>
    <TextBox x:Name="textBox3" IsEnabled="{Binding IsChecked, ElementName=radioButton3}"/>

I think you can change the Visibility:

textBox1.Visibility = Visibility.Visible;

I hope it works, it written from my mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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