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

 我来答
tianlidon
2015-02-09 · TA获得超过1219个赞
知道小有建树答主
回答量:676
采纳率:85%
帮助的人:102万
展开全部
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 设置选中的项!

匿名用户
推荐于2017-12-16
展开全部
一开始使用的是PreviewMouseDown解决,在MouseDown的时候,通过DataContext也好,通过FindAncestor也好,反正是在获得焦点的同时选择上了。 但是随着项目的进行,这种方法造成DataBinding的Validation出现了问题。Validation应该是在LostFocus是
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式