在表单加载时停止comboBox的selectedIndexChanged事件

Sta*_*ace 72 data-binding combobox winforms

我有一个ComboBox提供下拉列表的表单.在comboBox上SelectedIndexChanged event,运行一些代码,但我不希望代码在表单加载时运行.不幸的是,当我加载表格时(在我在组合框中进行选择之前),SelectedIndexChanged组合框发射(我认为当组合框时databinding).有没有办法避免这种行为?

arb*_*ter 138

如果您只想在用户更改组合框中的选定项目时做出反应,那么最好订阅SelectionChangeCommitted.

  • 但是在自动竞争的情况下该怎么办? (6认同)

Sha*_*pta 11

您可以简单地取消绑定SelectedIndexChanged事件,调用您的fill函数并SelectedIndexChanged再次绑定事件.不幸的是,这不适用于网格.

例如:

this.cmb.SelectionChanged -= new System.EventHandler(this.cmb_SelectionChanged);
cmb.fill(); //Your function
this.cmb.SelectionChanged += new System.EventHandler(this.cmb_SelectionChanged);
Run Code Online (Sandbox Code Playgroud)


小智 6

确定在分配和属性后DataSourceonload()函数中设置属性.ValueMemberDatamember

这将帮助您解决问题!

  • 请添加更多信息来阐述您的想法。 (2认同)

小智 5

为什么没有一个boolean标志表明你Form已经完成加载?

在您的SelectionChanged活动中,检查boolean标志是否true.如果它true然后处理事件,否则忽略它.

  • booooooolean .. SelectionChangeCommitted更好 (2认同)
  • 已经有一个布尔标志`Control.Created`告诉你,表单已经完成加载,这对表单也有效. (2认同)