当第一个组合框的选择发生变化时,如何更新secod组合框 [英] how to update the secod combobox when selection of first combobox changes
本文介绍了当第一个组合框的选择发生变化时,如何更新secod组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改列表,如果第二个组合框如果第一个组合框选择改变,我有两个组合框首先包含移动公司的名称,如htc三星和诺基亚,我想改变第二个组合框的列表,当第一个组合框选择更改
i已经完成了这个
i want to change the list if second combobox if the first combobox selection changes, i have two combobox first contains the names of mobile companies like htc samsung and nokia, and i want to change the list of second combobox when the first combobox selection changes
i have done this
string[] st2 = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedIndex == 1 )
{
comboBox2.Items = st2 // this is not happing here what can i do?
}
}
推荐答案
string[] samsung = new string[] { "Galaxy S", "Galaxy SII","Galaxy SIII" };
string[] apple = new string[] {"iPhone4", "iPhone4S"};
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if ("Samsung" == comboBox1.SelectedItem.ToString())
{
comboBox2.Items.AddRange(samsung);
}
else if("Apple" == comboBox1.SelectedItem.ToString())
{
comboBox2.Items.AddRange(apple);
}
}
如果它不起作用,请告诉我......
If its not working, let me know...
您需要将字符串添加到Items集合中:如何:添加和删除项目Windows窗体ComboBox,ListBox或CheckedListBox控件 [ ^ ]
You need to add the strings into the Items collection: How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control[^]
string[] st2 = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedIndex == 1 )
{
foreach( string st in st2 )
{
comboxBox2.Items.Add( st );
}
}
}
这篇关于当第一个组合框的选择发生变化时,如何更新secod组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文