2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WinFormsのComboBoxでSelected Changedイベントが何回も発火するのが嫌なら

Last updated at Posted at 2018-07-31

まずコード。
VB.NETで書いてあるやつを修正するとする。
すでに作成済のSelectedIndexChangedイベントの、Handles以降を消す。

Private Sub ComboBox1_Changed(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

Private Sub ComboBox1_Changed(sender As Object, e As EventArgs)

とする。

そして今回、私はLoadが終わったらイベントが発火していいので、Loadイベントの最後かShownイベントあたりで、

AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_Changed

としておく。

もしも、動的に行いたいのなら、イベントが発火してほしくないタイミングの前に

RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_Changed

でイベントをはがしておいて、後でまたAddHandlerすれば良い。

Formのロードイベントで、ComboBoxのItemsを設定しているような場合、何回もSelectedIndexChangedが動作しているようです。しかも、他のコントロールの影響で、コンボボックスのSelectedIndexが変わろうものなら、そこでまたSelectedIndexChangedが動作します。それが鬱陶しいと言われたので。
(バカみたいにステップ実行してるからじゃないか。。。とか思ったけど口には出さない)
(発火とかなんとかいまいち全体的に言葉が会ってるか自信ない)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?