【问题标题】:Excel VBA: Can't deselect ListBox items from listboxExcel VBA:无法从列表框中取消选择列表框项目
【发布时间】:2015-10-24 01:42:54
【问题描述】:

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

我有如下代码,但它似乎从未被调用:

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

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

非常感谢任何帮助!

谢谢,

罗斯

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以使用 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
    

    【讨论】:

    • 这似乎没有太大区别。当我在 ListBox 中单击时,没有取消选择任何内容。有什么想法吗?
    • 如果您复制粘贴代码,您需要将 ListBox1 的所有引用更改为 ListBox4 - 您可以发布您正在使用的代码吗?
    • 它是这样写的,它只有在 ListBox 获得焦点之前按下 CommandButton 时才会取消选择所有内容。您想要不同的功能吗?
    • 这就是我想要的功能。有一个命令首先被调用,我在那里设置布尔值,就像在你的 CommandButton1_Click 中一样。我更改了您发送的代码,所以它说的是 ListBox4,而不是 1。事实上,当我在 ListBox4_GotFocus() 中为代码设置断点时,它永远不会到达它们。
    • 这很奇怪,每次 ListBox 重新获得系统焦点时都会触发该事件。您可以将文件发布到某个地方以便我可以打开它吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多