[英]vb: How to select multiple items in a Listbox at once?
I have a Listbox with a list of numbers from 1 to 10. Now I want to program to select those numbers greater than 5. But I also want to trigger the SelectedIndexChanged event only once. 我有一个列表框,其中包含从1到10的数字。现在,我要编程选择大于5的那些数字。但是,我也只想触发一次SelectedIndexChanged事件。
I know I can add multiple items into Listbox at once by using addrange() method. 我知道我可以使用addrange()方法一次将多个项目添加到列表框中。
But it seems there isn't a similar solution for select multiple item at once ? 但是似乎没有类似的解决方案可以一次选择多个项目吗?
How can i do this ? 我怎样才能做到这一点 ?
youre question is a litle unclear but ... 您的问题尚不清楚,但...
First you need to set your Listbox SelectionMode to MultySimple. 首先,您需要将Listbox SelectionMode设置为MultySimple。
Then you use ListBox1.SelectedItems.Count < 2 So it trigger the SelectedIndexChanged event only once at the start of the selecting. 然后,使用ListBox1.SelectedItems.Count <2,因此它仅在选择开始时触发一次SelectedIndexChanged事件。
Of course you can edit the code to fit your needs and let it trigger whenever you want. 当然,您可以根据需要编辑代码,并在需要时触发它。
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count < 2 Then
MsgBox("one")
End If
End Sub
And to select everything above 5 you need to make a list of integers. 要选择大于5的所有内容,您需要制作一个整数列表。
Then use a for each loop to get the items in the list of integers with the values you want. 然后对每个循环使用a,以获取具有所需值的整数列表中的项目。
Then use the list of integers in a for loop to select the items in the listbox. 然后在for循环中使用整数列表在列表框中选择项目。
Dim l As New List(Of Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Item As Integer In ListBox1.Items
If Item > 5 Then
l.Add(ListBox1.FindString(Item))
End If
Next
For SetItem As Integer = 0 To l.Count - 1
For i = 0 To ListBox1.Items.Count - 1
If i = l.Item(SetItem) Then
ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.