[英]how can we enable combobox based on the value selected in another combo box in C# winform
我们如何根据在 C# winform 的另一个组合框中选择的值启用 combobox。
我有两个组合框,如下所示。 需要根据在第一个组合框中选择的值启用第二个组合框。 请注意,combobox1 的第一个值是默认值,需要禁用 combobox 2,但如果选择了任何其他值,则应启用 combobox 2。
假设你有 2 个组合框, combobox1
和combobox2
首先将事件侦听器添加到combobox1
。 从那里,您可以检查您的选项是否已被选中
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.