如何正确地用代码设置ListBox的当前选中项

如何正确地用代码设置ListBox的当前选中项,第1张

Private Sub RemoveTopItems()

   ' Determine if the currently selected item in the ListBox 

   ' is the item displayed at the top in the ListBox.

   If listBox1.TopIndex <> listBox1.SelectedIndex Then

      ' Make the currently selected item the top item in the ListBox.

      listBox1.TopIndex = listBox1.SelectedIndex

   End If

   ' Remove all items before the top item in the ListBox.

   Dim x As Integer

   For x = listBox1.SelectedIndex - 1 To 0 Step -1

      listBox1.Items.RemoveAt(x)

   Next x

   ' Clear all selections in the ListBox.

   listBox1.ClearSelected()

End Sub 'RemoveTopItems

listBox1.SelectedIndex 设置选中的项!

            for (int i = 0 i < listBox1.Items.Count i++)

            {

                listBox1.SelectedItem = listBox1.Items[i]

            }

首先要将listBox的selectionMode属性设置为MultiSimple或者MultiExtended后执行上述代码即可,不过如果你只是想做列表中的多项选择功能的话推荐用CheckedListBox控件,更加方便

欢迎追问


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/11162710.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存