简体   繁体   English

我们如何根据在 C# winform 的另一个组合框中选择的值启用 combobox

[英]how can we enable combobox based on the value selected in another combo box in C# winform

how can we enable combobox based on the value selected in another combo box in C# winform.我们如何根据在 C# winform 的另一个组合框中选择的值启用 combobox。

I have two combo box as shown below.我有两个组合框,如下所示。 Need to enable second combo box based on the value selected in first combo box.需要根据在第一个组合框中选择的值启用第二个组合框。 Note that the first value of combobox1 is defualt value and combobox 2 needs to be disabled but if any other value is selected the combobox 2 should be enabled.请注意,combobox1 的第一个值是默认值,需要禁用 combobox 2,但如果选择了任何其他值,则应启用 combobox 2。

enter image description here在此处输入图像描述

Say you have 2 combo boxes, combobox1 and combobox2假设你有 2 个组合框, combobox1combobox2

Start by adding an event listener to combobox1 .首先将事件侦听器添加到combobox1 From there, you can check whether your option has been selected从那里,您可以检查您的选项是否已被选中


        comboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //
                // Get the source of the component, which is our combo
                // box.
                //
                JComboBox comboBox = (JComboBox) event.getSource();

                Object selected = comboBox.getSelectedItem();
                if(selected.toString().equals("item1"))
                    combobox2.Enabled = True;
                else if(selected.toString().equals("item2"))
                    combobox2.Enabled = False;


            }
        });

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

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