LoginSignup
1
0

More than 3 years have passed since last update.

Windowsフォーム コンボボックスのSelectIndexの不思議な動き

Posted at

以下のような単純なコンボボックスで、「千葉」を選び・・・
image.png

閉じて・・・
image.png

▼を押して開くと・・・
image.png

なぜか、「千葉県」が選択されています。
「千葉」を内包するアイテムが「千葉」より前にある場合(この場合
「千葉県」「千葉市」)に、そのアイテムが選択されてしまうようです。

しかし
image.png
「真一千葉」は選択されません。

このことから「千葉」を前方一致で内包するアイテムが選択されてしまうということがわかります。

.NET 4.8 系で検証しました。

Public Class test
    Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
        Console.WriteLine("DropDown " & ComboBox1.SelectedIndex)
    End Sub

    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
        Console.WriteLine("DropDownClosed " & ComboBox1.SelectedIndex)
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Console.WriteLine("SelectionChangeCommitted " & ComboBox1.SelectedIndex)
    End Sub
End Class

前述した動作をすると、コンソールには

SelectionChangeCommitted 7 // 千葉
DropDownClosed 7
DropDown 7
DropDownClosed 3 // 千葉県

と出力されます

1
0
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
1
0