简体   繁体   English

在comboBox值上启用/禁用控件

[英]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: 看起来应该一直启用您的comboBoxToStillShowCheckboxToStillShow ,因此只需在循环中跳过它们即可:

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;
    }        
}

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

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