combobox SelectedIndexChanged事件 如何触发

时间:2024-02-21 18:48:35
 
 
 
l         SelectionChangeCommitted事件
在设定datasource和displaymember和valuemember时,以及手动改变combobox索引和值时都不触发该事件, 只是在界面选择combobox不同值时触发
2       SelectedIndexChanged事件  索引改变时触发
3         SelectedValueChanged事件   值改变时触发

设置DataSource, DisplayMember, ValueMember不同顺序触发SelectedIndexChanged事件和SelectedValueChanged事件的次数

顺序一:

this.comboBox1.DisplayMember = "userName";  (不触发)

this.comboBox1.ValueMember = "userAge";    (只触发SelectedValueChanged)

this.comboBox1.DataSource = dt; (都触发)

SelectedValueChanged触发两次(总是先触发)SelectedIndexChanged,触发一次 (设置DisplayMember属性时不触发两种事件)



顺序二:

this.comboBox1.ValueMember = "userAge";

this.comboBox1.DisplayMember = "userName";

this.comboBox1.DataSource = dt;

同上



顺序三:

this.comboBox1.DataSource = dt; (都触发)

this.comboBox1.DisplayMember = "userName";(都触发)

this.comboBox1.ValueMember = "userAge";(只触发SelectedValueChanged事件)

SelectedValueChanged触发三次(总是先触发)SelectedIndexChanged,触发两次 (设置DisplayMember属性时不触发两种事件)





顺序四:

this.comboBox1.DataSource = dt; (都触发)

this.comboBox1.ValueMember = "userAge";(都触发) 其中SelectedValueChanged触发两次

this.comboBox1.DisplayMember = "userName";(都触发)

SelectedValueChanged触发四次(总是先触发)SelectedIndexChanged,触发三次 (设置DisplayMember属性时不触发两种事件)