当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Issue selecting only one item with multiple ListBoxes

问题描述:

I'm new to VB.NET and I've tried to create a simple application. My need is to create a form with some listboxes and I want to have only one item selected at the same time. I've written this code in the Form1 Class:

Public Class Form1

Private Sub aggiornaQKI(sender As Object, e As System.EventArgs) _

Handles ListBox1.Click, ListBox9.Click, ListBox8.Click, ListBox7.Click, _

ListBox6.Click, ListBox5.Click, ListBox4.Click, ListBox3.Click, ListBox2.Click

If sender.selectedindex >= 0 Then

If sender.name <> "ListBox1" Then ListBox1.SelectedIndex = -1

If sender.name <> "ListBox2" Then ListBox2.SelectedIndex = -1

If sender.name <> "ListBox3" Then ListBox3.SelectedIndex = -1

If sender.name <> "ListBox4" Then ListBox4.SelectedIndex = -1

If sender.name <> "ListBox5" Then ListBox5.SelectedIndex = -1

If sender.name <> "ListBox6" Then ListBox6.SelectedIndex = -1

If sender.name <> "ListBox7" Then ListBox7.SelectedIndex = -1

If sender.name <> "ListBox8" Then ListBox8.SelectedIndex = -1

If sender.name <> "ListBox9" Then ListBox9.SelectedIndex = -1

End If

End Sub

End Class

How it's possible that I have more than one item selected at runtime? The only thing that I do is to click between listboxes items very quickly!

I have no reputation to post an image but I can guarantee that is like I've said. I've tried to run the application on another pc but is the same.

I have Windows 7 64 bit and .NET 4.0.

In a new version, I've written a Console.WriteLine(sender.name) before the first if. When the issue occur, it seems that the last click event was lost because I don't find the name of the last listbox clicked!

To confirm this, when the two items are selected, if I insert a breakpoint and go to see the value of the selecteditem property of the listboxes, I see effectively that two of that have values <> -1.

网友答案:

The event you may want to use is SelectedIndexChanged. So instead of

handles Listbox1.Click

try

handles Listbox1.SelectedIndexChanged, Listbox2.SelectedIndexChanged
分享给朋友:
您可能感兴趣的文章:
随机阅读: