本文介绍了自动更新一个组合框,当第一个组合框中获取一定的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个组合框。我在插入第一个组合框中一个值,现在我想,我的第二个组合框更新根据第一个它的价值。我应该怎么做呢?
I have two combo boxes. I insert one value in first combo box and now i want that my second combo box updates its value according to first one. How should i do that?
推荐答案
在处理的事件的第一个组合框
,然后更新基于该第一个组合框
值。
Handle the SelectedIndexChanged
event for the first ComboBox
, then update the second combo box based on the SelectedItem
value for the first ComboBox
.
一个简单的例子(没有错误处理检索的SelectedItem时):
A quick example (sans error handling when retrieving the SelectedItem):
public partial class Form1 : Form
{
private string[] comboBox1Range = new[] { "A", "B", "C", "D" };
private string[] comboBox2RangeA = new[] { "A1", "A2", "A3", "A4" };
private string[] comboBox2RangeB = new[] { "B1", "B2", "B3", "B4" };
private string[] comboBox2RangeC = new[] { "C1", "C2", "C3", "C4" };
private string[] comboBox2RangeD = new[] { "D1", "D2", "D3", "D4" };
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox1.Items.AddRange(comboBox1Range);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = comboBox1.SelectedItem as string;
switch (selectedValue)
{
case "A":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeA);
break;
case "B":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeB);
break;
case "C":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeC);
break;
case "D":
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2RangeD);
break;
}
}
}
这篇关于自动更新一个组合框,当第一个组合框中获取一定的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!