简体   繁体   English

为什么ComboBox.SelectedIndexChanged事件在加载表单之前触发三次?

[英]Why does ComboBox.SelectedIndexChanged event fire three times before form load?

I have a little problem with combobox databinding. 我对组合框数据绑定有一点问题。 I have a form that has one combobox and based on the selection in the ComboBox the form should be filled with data. 我有一个具有一个组合框的表单,并且根据ComboBox中的选择,表单应该填充数据。 I use the SelectedIndexChanged event instead of SelectionChangeCommitted because I want to be able to fill the form with the data of the first item in the ComboBox when the form loads. 我使用SelectedIndexChanged事件而不是SelectionChangeCommitted事件,因为我希望能够在加载表单时用ComboBox中第一项的数据填充表单。 I have tried 2 solutions: 我尝试了2个解决方案:

  1. Add the event handler before Data Binding. 在数据绑定之前添加事件处理程序。

This solution almost works as it should. 该解决方案几乎可以正常工作。 It selects the first ComboBox item and fills the form with relevant data but for some reason this event fires three times when binding data and this makes the form loading slow because some data gets called from a database and basically this data is being loaded 3 times. 它选择第一个ComboBox项并用相关数据填充表单,但是由于某种原因,绑定数据时此事件将触发3次,这将导致表单加载速度变慢,因为从数据库中调用了某些数据,并且基本上此数据已加载3次。

  1. Add the event handler after Data Binding. 在数据绑定之后添加事件处理程序。

This solution doesn't work like I'd want to because it doesn't fill the form with the relevant data although the first item is correctly selected in the ComboBox after form load. 尽管在加载表单后在ComboBox中正确选择了第一项,但该解决方案无法像我想要的那样工作,因为它没有用相关数据填充表单。 This is mainly because I add the event handler (fills the form with data) after the Data Binding is done. 这主要是因为我在完成数据绑定之后添加了事件处理程序(用数据填充表单)。 The good thing about this solution is that the event is not being fired 3 times in a row, but that's the only good thing about this solution because the form is not being filled with relevant data like intended. 此解决方案的优点是事件不会连续触发3次,但这是此解决方案的唯一优点,因为该表单未填充预期的相关数据。

Does anyone have any idea why this event is getting fired 3 times? 有谁知道为什么此事件被触发3次? As a possible way to fix the problem I could call the method, that should be fired by the event, after Data Binding and then add event handler to the ComboBox, but this looks like a hackish way of doing things. 作为解决问题的一种可能方法,我可以调用该方法,该方法应在数据绑定之后由事件触发,然后将事件处理程序添加到ComboBox,但这看起来像是一种骇人听闻的工作方式。 If someone can provide a better way to get things done I'd be really happy. 如果有人可以提供更好的方法来完成任务,我将非常高兴。 However, if my solution happens to be the only/best way of doing things, then can someone maybe tell me technical reasons for the event firing multiple times during Data Binding? 但是,如果我的解决方案恰好是唯一/最好的处理方式,那么有人可以告诉我在数据绑定期间多次触发事件的技术原因吗?

EDIT: I guess I found the reason and solution already. 编辑:我想我已经找到了原因和解决方案。 The reason the event got fired 3 times wasn't that data binding fired it 3 times - it was because assigning DisplayMember and ValueMember fired this event too. 事件被触发3次的原因不是数据绑定触发了3次-而是因为分配DisplayMember和ValueMember也触发了该事件。

Before: 之前:

combo.SelectedIndexChanged += new System.EventHandler(this.combo_SelectedIndexChanged);
combo.DataSource = getItems().ToList();  
combo.ValueMember = "Key";  
combo.DisplayMember = "Value";  

After: 后:

combo.ValueMember = "Key";
combo.DisplayMember = "Value";  
combo.SelectedIndexChanged += new System.EventHandler(this.combo_SelectedIndexChanged);
combo.DataSource = getItems().ToList();

If this issue comes up when you're loading the winform and the combobox's datasource at the same time then check the combobox.Focused value. 如果在同时加载winform和combobox的数据源时出现此问题,请检查combobox.Focused值。 If the datasource is being defined when the form loads then there's been no true selection made so the object doesn't have the focus. 如果在加载表单时定义了数据源,则没有进行真正的选择,因此对象没有焦点。 So in the selectionChanged event just add. 因此,在selectionChanged事件中只需添加即可。 if(combobox.Focused == true) { //do something } if(combobox.Focused == true){//做某事}

and then the problem goes away... 然后问题就解决了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Winforms:哪个事件要触发? combobox.selectedindexchanged或bindingsource.currentchanged - Winforms: Which event to fire? combobox.selectedindexchanged OR bindingsource.currentchanged 以编程方式更改索引时,不会触发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged does not fire when programatically changing the index Combobox.SelectedIndexChanged的行为 - Behavior of Combobox.SelectedIndexChanged 不引发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged is not raised 为什么DropDownList.SelectedIndexChanged事件不会触发? - Why DropDownList.SelectedIndexChanged event does not fire? 如何在 comboBox.SelectedIndexChanged 事件中更改 comboBox.Text? - How do I Change comboBox.Text inside a comboBox.SelectedIndexChanged event? 为什么事件:comboBox_SelectedIndexChanged在form_load事件中触发? - why the event: comboBox_SelectedIndexChanged get trigered at form_load event? 无法为组合框中的单个项目触发SelectedIndexChanged事件 - Unable to fire SelectedIndexChanged Event for a single item in a combobox 为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件? - Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified? Windows Form DataGridView将SelectedIndexChanged事件附加到ComboBox - Windows Form DataGridView attach SelectedIndexChanged event to ComboBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM