VB.net空错误

VB.net空错误,vb.net,null,argumentnullexception,Vb.net,Null,Argumentnullexception,我在尝试处理ArgumentNullExection时遇到问题 值不能为空 参数名称项 到目前为止,我尝试了以下方法 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ArgumentNullException As New Boolean If (ListBox2.Items.Add(Lis

我在尝试处理ArgumentNullExection时遇到问题

值不能为空

参数名称项

到目前为止,我尝试了以下方法

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ArgumentNullException As New Boolean
        If (ListBox2.Items.Add(ListBox1.SelectedItem.ToString = "")) Then
           MessageBox.Show("please pick from list", "error")
        End If
        ListBox1.Items.Remove(ListBox1.SelectedItem)

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ArgumentNullException As New Boolean
        If (ListBox2.Items.Add(ListBox1.SelectedItem Is Nothing)) Then
           MessageBox.Show("please pick from list", "error")
        End If
        ListBox1.Items.Remove(ListBox1.SelectedItem)


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ArgumentNullException As New BooleanListBox2
        ListBox2.Items.Add(ListBox1.SelectedItem)
        If (ListBox1.SelectedItem) = ""
           MessageBox.Show("please pick from list", "error")
        ListBox1.Items.Remove(ListBox1.SelectedItem)
还有错误,有人能帮忙吗

更新:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox2.Items.Add(ListBox1.SelectedItem)
            If IsNothing (ListBox1.SelectedItem)
               MessageBox.Show("please pick from list", "error")
            ListBox1.Items.Remove(ListBox1.SelectedItem)

给出错误仍然

问题很不清楚,但我会尝试一下


当您的应用程序具有
列表框
并使用
.SelectedItem
属性时,您应该始终使用Try/Catch,甚至更好地检查用户是否选择了以下项:

If IsNothing(ListBox1.SelectedItem) Then Exit Sub
你也可以使用

If ListBox1.SelectedItems.Count = 0 Then Exit Sub
您也可以使用If/Else,但在我看来,将语句放在代码的第一行可以使它更具可读性

当没有选定的项目时,使用此代码将使按钮不执行任何操作。 否则,您可以使用多行If来放置
MsgBox(“请选择项目”)
,以警告用户:

If IsNothing(ListBox1.SelectedItem) Then
    MsgBox("Please pick an Item first.")
    Exit Sub
End If
// Your Code after that


您最好粘贴整个相关代码,例如列表变量的初始化。这到底是什么:
If(ListBox2.Items.Add(ListBox1.SelectedItem.ToString=”“)然后
?Noob试用和错误我读了一篇不起作用的帖子,然后就尝试了不同的组合。我的目标是,如果用户只是按下按钮而没有选择项目,消息框就会出现感谢错过了结尾子项很高兴我能帮上忙。始终记住:打开的所有内容都需要在某个位置关闭:If/End If、While/EndWhile、Function/End Function、Sub/End Sub、Select Case/End Select、(…)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  // Leave the Sub and Message the User, when there is no SelectedItem
  If IsNothing(ListBox1.SelectedItem) Then
      MsgBox("Please pick an Item first.")
      Exit Sub
  End If
  // Code will only reach here, when there is a SelectedItem
  ListBox2.Items.Add(ListBox1.SelectedItem)
  ListBox1.Items.Remove(ListBox1.SelectedItem)
End sub