如何在填充列表框时抑制indexchanged事件? [英] How so suppress the indexchanged event when filling a listbox?

查看:87
本文介绍了如何在填充列表框时抑制indexchanged事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



在我的代码中,我使用了listboxes的SelectedIndexChanged事件。我不得不发现,当列表框中填充了它的元素时,这个事件似乎会自动引发。



首先,在填充列表框之后我想要没有选择任何项目。我现在用ctrl.SelectedItem = Nothing做这个,但不知怎的,我觉得这不是很优雅。也许你有一个更好的想法或良好的做法,我可以学习这个。



我的问题的主要关注点是:有没有办法抑制SelectedIndexChanged事件,而列表框还没有填写?那会是什么?



感谢您的任何提示。



亲切的问候

Michael



我的尝试:



Hello,

In my code I'm using the SelectedIndexChanged event of listboxes. I had to find out that this event seems to be automatically raised while the listbox gets filled with it's elements.

First of all, after the listbox has been filled I want to have no item selected. I'm currently doing this with "ctrl.SelectedItem = Nothing", but somehow I have the feeling that's not really elegant. Maybe you have a better idea or good practice I can learn on this.

The main concern of my question is: Is there a way to suppress the SelectedIndexChanged event while the listbox hasn't been filled? Which would that be?

Thank you for any hints.

Kind Regards
Michael

What I have tried:

Private Sub lstServers_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstServers.SelectedIndexChanged
      Dim ctrl As ListBox = DirectCast(sender, ListBox)

      ' SelectedItem was set to Nothing beforehand
      If ctrl.SelectedItem IsNot Nothing then
      ... do something ...
      End If
End Sub

推荐答案

您可以从处理程序列表中删除该事件并将其添加回最后:在VB.NET中动态添加和删除事件处理程序 [ ^ ]在填充时有效地禁用它。
You could remove the event from the handlers list and add it back at the end: Add and remove event handlers dynamically in VB.NET[^] That effectively "disables" it while it's being filled.


这篇关于如何在填充列表框时抑制indexchanged事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆