简体   繁体   中英

Enabling/Disabling controls on comboBox value

I have a row of Combo boxes with values that should enable/disable when a certain option is selected in the operator select box. The problem is the code disables all the controls in the panel and the form has to be reloaded to reactive said controls. When the select combobox has 'none' selected i want it to disable all controls BUT my second combobox and also a checkbox. Any help with the code is much appreciated.

private void OperatorSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        bool isEnabled = 
            (OperatorSelect.SelectedItem.ToString() != "(None)");
        foreach (Control cb in this.Controls)
        {
            cb.Enabled = !isEnabled;

        }
        this.comboBoxToStillShow = !isEnabled;
        this.CheckboxToStillShow = !isEnabled;

    }     

Currently all the controls are disabled, i need help with the two other controls to still be enabled and all the rest to be disabled. Thank you

Looks like your comboBoxToStillShow and CheckboxToStillShow should be enabled all the time, so just skip them in the loop:

private void OperatorSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    bool isEnabled = 
        (OperatorSelect.SelectedItem.ToString() != "(None)");
    foreach (Control cb in this.Controls)
    { 
        if(cb == comboBoxToStillShow || cb == CheckboxToStillShow) continue;
        cb.Enabled = !isEnabled;
    }        
}

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