Comobox event SelectedValueChanged

谁说胖子不能爱 提交于 2019-12-21 03:46:15

问题


i have simple question may be someone asked it before me but i could not find it.Let say i have datatable that has some data from the database and i want to bind it to a combobox i use standart code like this

 comboBox1.BeginUpdate( );
 comboBox1.ValueMember = "id";
 comboBox1.DisplayMember = "name";
 comboBox1.DataSource = dt;
 comboBox1.EndUpdate( );

The problem is during this binding the event SelectedValueChanged is fired.The problem is that rebind combo several times when outher values change and every time i must do sometihn like this

 comboBox1.SelectedIndexChanged -= new System.EventHandler( this.comboBox1_SelectedValueChanged );

my question is there a smarter way to skip the event when i comes from databinding not from user input.The problem is that i want to do it some how globaly in my control that inherits combobox and not to do it everytime in every from Best Regards,
Iordand


回答1:


Try using the SelectionChangeCommitted event.

From MSDN documentation:

SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically.




回答2:


I've always done as space cracker said. I create a global boolean variable named _isLoading and set it to true while loading my combobox, then back to false when it's done. Then in the event handler the first line is

if(_isLoading) return;


来源:https://stackoverflow.com/questions/2793207/comobox-event-selectedvaluechanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!