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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15304509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:55:52  来源:igfitidea点击:

Excel VBA: Can't deselect ListBox items from listbox

excel-vbavbaexcel

提问by Russ Urquhart

I have a command button that processes selected items in a ListBox (ListBox4). While i CAN deselect all the items in that command's Click() procedure, I wanted to, if the user clicked in the ListBox, to THEN deselect everything in the ListBox, prior to their selecting again.

我有一个命令按钮,用于处理 ListBox (ListBox4) 中的选定项目。虽然我可以取消选择该命令的 Click() 过程中的所有项目,但我想,如果用户在 ListBox 中单击,则在再次选择之前取消选择 ListBox 中的所有内容。

I have code like the following, but it never seems to get called:

我有如下代码,但它似乎永远不会被调用:

Private Sub ListBox4_Click()
If Apply_Format_Occurred Then
For i = 0 To ListBox4.ListCount - 1
         ListBox4.Selected(i) = False
Next i
End Sub

Do i need an outside command, etc to do this? I was hoping to be able to do it like how i described.

我是否需要外部命令等来执行此操作?我希望能够像我描述的那样做。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

thanks,

谢谢,

Russ

拉斯

回答by Ross McConeghy

You can use the GotFocus event of the ListBox so the code runs when the ListBox receives focus from the user.
Here is an example showing coding for the button and the ListBox:

您可以使用 ListBox 的 GotFocus 事件,以便在 ListBox 接收到来自用户的焦点时运行代码。
这是显示按钮和列表框编码的示例:

Dim Apply_Format_Occurred As Boolean

Private Sub CommandButton1_Click()
    '<other processes>
    Apply_Format_Occurred = True
End Sub

Private Sub ListBox4_Change()
    If Apply_Format_Occurred Then
        For i = 0 To ListBox4.ListCount - 1
             ListBox4.Selected(i) = False
        Next i
        Apply_Format_Occurred = False
    End If
End Sub

回答by Mind's Eye Fab

I see this thread is old and maybe there is an easy or more elegant way to un-select a list box item. But, I figured out a way for my needs. In my case I wanted the list box to un-select only if the same item was clicked. If a new item was clicked, it would be selected as normal. I had to use two static variable (a boolean and "old selection" placeholder).

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

There's probably a much easier way. But, I'm new to it and and couldn't find it. Hope this helps. Setting selectedindex to -1 unselects everything. Altenatively and as effective is listboxName.ClearSelected()

可能有更简单的方法。但是,我是新手,找不到它。希望这可以帮助。将 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