Excel VBA:无法从列表框中取消选择ListBox项

您所在的位置:网站首页 vba下拉列表框 Excel VBA:无法从列表框中取消选择ListBox项

Excel VBA:无法从列表框中取消选择ListBox项

#Excel VBA:无法从列表框中取消选择ListBox项| 来源: 网络整理| 查看: 265

我发现这个线程很老了,也许有一种简单或者更优雅的方法来取消选择列表框项目。但是,我想出了一个方法来满足我的需求。在我的例子中,我希望列表框仅在相同的项目被单击时才取消选中。如果单击了一个新项目,它将被正常选择。我不得不使用两个静态变量(一个布尔值和一个“旧选择”占位符)。

可能有一种更简单的方法。但是,我是个新手,并且找不到它。希望这能有所帮助。将selectedindex设置为-1将取消选择所有内容。listboxName.ClearSelected()也同样有效

Private Sub lstPendingQuotes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPendingQuotes.Click Static blnSelectable As Boolean = True 'use static boolean to retain value between calls Static objOldSelected As Object = lstPendingQuotes.SelectedIndex 'use old selected in case a different index is selected 'if nothing is already selected (as in first form open) allow the selection but change boolean to false and set the OldSelected variable to the current selection If blnSelectable Then blnSelectable = False objOldSelected = lstPendingQuotes.SelectedIndex ElseIf lstPendingQuotes.SelectedIndex = objOldSelected Then 'if an item is selected and the same item is clicked un-select all and reset boolean to true for a new possible selection lstPendingQuotes.SelectedIndex = -1 'can substitute lstPendingQuotes.ClearSelected() blnSelectable = True Else 'If a different index is chosen allow the new selection and change boolean to false so if the same item is clicked it will un-select objOldSelected = lstPendingQuotes.SelectedIndex blnSelectable = False End If End Sub


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3