问题描述
在winform中加两个combox,第一个设置两个值a和b,选择a时combox2下拉中显示c和d,combox1中选择b时,combox下拉列表出现m和n而不出现c和d,具体怎么做啊?具体代码怎么写啊?还有一个问题,selected.indexchanged和valuechanged什么区别?菜鸟刚接触,虚心向大侠求教
解决方案
解决方案二:
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){switch(comboBox1.SelectedIndex){case0:comboBox2.Items.Clear();comboBox2.Items.AddRange(newstring[]{"c","d"});comboBox2.SelectedIndex=0;break;case1:comboBox2.Items.Clear();comboBox2.Items.AddRange(newstring[]{"m","n"});comboBox2.SelectedIndex=0;break;}}
区别http://www.cnblogs.com/gossip/archive/2008/12/10/1352193.html
解决方案三:
@cnfixit:case0和case1指的是combox1中a和b的顺序么?case语句中为什么combox2的changeindex要等于零?谢谢
解决方案四:
引用2楼的回复:
@cnfixit:case0和case1指的是combox1中a和b的顺序么?case语句中为什么combox2的changeindex要等于零?谢谢
是的,设置combox2的changeindex=0是让combobox2默认选择第一项
解决方案五:
引用1楼的回复:
C#codeprivatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){switch(comboBox1.SelectedIndex){case0:……
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){switch(comboBox1.Text){case"a":comboBox2.Items.Clear();comboBox2.Items.AddRange(newstring[]{"c","d"});comboBox2.SelectedIndex=0;break;case"b":comboBox2.Items.Clear();comboBox2.Items.AddRange(newstring[]{"m","n"});comboBox2.SelectedIndex=0;break;}}楼主这样是否更清楚?
解决方案六:
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){comboBox2.Items.Clear();switch(comboBox1.Text){case"a":comboBox2.Items.AddRange(newstring[]{"c","d"});break;case"b":comboBox2.Items.AddRange(newstring[]{"m","n"});break;}comboBox2.SelectedIndex=0;}
简化下,,,
解决方案七:
privateSortedList<string,string[]>slist=newSortedList<string,string[]>(2){{"a",newstring[]{"c","d"}},{"b",newstring[]{"m","n"}}};privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){string[]arr;this.slist.TryGetValue((string)comboBox1.SelectedItem,outarr);comboBox2.DataSource=arr;}