Checkedlistbox 等于没有问题。

Checkedlistbox equals nothing issue.

如果有如下代码:

For Each item As Object In Me.CheckedListBox2.CheckedItems
        Dim PT As String = Me.CheckedListBox2.GetItemText(item)
If PT = "Port 1" Then
            Port1.Sendkeys("command")
            Port1.Sendkeys("{Enter}")
            OtherSub()
        End If

这部分是为了让我可以将每个项目放入清单框中并将它们添加到命令列表中。

现在我需要做一个 If 语句,如果没有选择任何东西,它将 运行 作为默认命令。

 If CheckedListBox2.SelectedValue = Nothing Then
            Reset()
            MsgBox("Wrong")
        End If

这是我最后一次尝试以及我从各种代码示例(我能找到的)和论坛中尝试过的其他几次尝试。 None 将执行 Reset sub 或 MsgBox。

其他尝试:

If PT = "" Then
If PT = Nothing Then
If PT = " " Then
If CheckedListBox2.SelectedIndex = -1 Then

如有任何帮助,请多多指教。

使用Is Nothing代替= Nothing

 If CheckedListBox2.SelectedValue Is Nothing Then

 End If

我还建议始终将 Option Strict 设置为 ON。那么 = Nothing 甚至不会针对引用类型进行编译,这是一件好事。

What is the difference between 'foo = Nothing' and 'foo is Nothing' in VB.NET?