在 Visual Basic 中调用子例程后禁用进一步的列表框输入

Disable further listbox input after subroutine is called in Visual Basic

Here is my subroutine which is called by double clicking text in the listbox "LstAnswer4" I have 4 listboxes on the form and want to disable any further inputs after double clicking one of them.

    Public Sub LstAnswer4_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstAnswer4.MouseDoubleClick
    If res4 = 2 Then
        MsgBox("correct")
        Score = Score + 1
        ListBox1.Items.Clear()
        Dim displayscore As Object
        displayscore = (Score & " out of " & outof)
        ListBox1.Items.Add(displayscore)
    Else MsgBox("wrong")
    End If
        End Sub

我们可以像这样禁用列表框:

Listbox1.Enabled=false
Me.Enabled = False

非常安全:)

假设您想禁用所有 4(+) 个列表框,有几种方法可以解决此问题。

如果您想禁用表单上的 所有 列表框,您可以通过禁用所有类型为列表框的表单控件来实现:

    For Each Contr As Control In Me.Controls
        If Contr.GetType() = GetType(ListBox) Then
            Contr.Enabled = False
        End If
    Next

如果表单上有您不想禁用的列表框,但您禁用的列表框的名称相似(例如:LstAnswerx),您可以使用 wildcard/StartsWith 以匹配控件名称的开头:

    For Each Contr As Control In Me.Controls
        If Contr.GetType() = GetType(ListBox) And Contr.Name.StartsWith("LstAnswer") Then
            Contr.Enabled = False
        End If
    Next