简体   繁体   中英

How to enable or disable a combo box based on a radio button's value C#

I have a simple stupid question but I just can get the hang of it...So i have a form in C#, with a combo box button and also a radio button. I want when selected a value from radio button that the combo box to be disabled, if not the combo box to be enabled...

Any hints? Many thanks in advance,

I would suggest subscribing to the radioButton checked change event, then as Sudhakar suggested update the combo box enabled property according to what you want to happen.

So for example;

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    comboBox1.Enabled = !radioButton1.Checked;
}

if you want to Enable/Disable Combobox based on the RadioButton Checked status you can check for Checked property of the RadioButton and Enable/Disable the Combobox .

Checked Property returns the boolean value, so if it is true you can disable the ComboBox .

if it is false you can Enable the ComboBox .

Note : Handle the RadioButton CheckedChanged Event to handle it properly. Try This :

 private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
           if (radioButton1.Checked)
                comboBox1.Enabled = false;
            else
                comboBox1.Enabled = true;
     }

You can use DataBinding to acheive this.
The result may look like this:

public Form1()
    {
        InitializeComponent();

        comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("Enabled", radioButton1, "Checked", true, DataSourceUpdateMode.OnPropertyChanged));
    }

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