标签:size combobox 用户 方式 display code += exchange 编程
SelectedIndexChanged和SelectedValueChanged可以通过编程更改属性来触发事件,SelectionChangeCommitted事件必须由用户操作才能触发。
在初始化时(设置源)SelectedIndexChanged和SelectedValueChanged都会被调用。
在不同的执行顺序下,触发的情况也不同:
顺序一:
this.comboBox1.DisplayMember = "userName";(不触发) this.comboBox1.ValueMember = "userAge";(只触发SelectedValueChanged) this.comboBox1.DataSource = dt;(都触发)
顺序二:
this.comboBox1.ValueMember = "userAge";(只触发SelectedValueChanged)
this.comboBox1.DisplayMember = "userName";(不触发)
this.comboBox1.DataSource = dt; (两者都触发)
顺序三:
this.comboBox1.DataSource = dt; (都触发)
this.comboBox1.DisplayMember = "userName";(都触发)
this.comboBox1.ValueMember = "userAge";(只触发SelectedValueChanged事件)
顺序四:
this.comboBox1.DataSource = dt; (都触发)
this.comboBox1.ValueMember = "userAge";(都触发,其中SelectedValueChanged触发两次)
this.comboBox1.DisplayMember = "userName";(都触发)
避免触发事件的方式有以下两种:
int temp = 0; // code omited temp++; if(temp > 3) { // code omited }
comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;//删除事件
// code omited
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;//重新订阅事件
标签:size combobox 用户 方式 display code += exchange 编程
原文地址:https://www.cnblogs.com/jizhiqiliao/p/9910317.html