VB.net 空错误
VB.net Null error
我在尝试处理 ArgumentNullExeption 发生时遇到问题
值不能为 Null
参数名称项目
到目前为止我尝试了以下方法
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)
仍然报错
问题不太清楚,我试试看。
当您的应用程序具有 Listboxes
并使用 .SelectedItem
属性时,您应该始终使用 Try/Catch 或更好地检查用户是否选择了项目:
If IsNothing(ListBox1.SelectedItem) Then Exit Sub
您也可以使用
If ListBox1.SelectedItems.Count = 0 Then Exit Sub
您也可以使用 If/Else,但在我看来,将语句放在代码的第一行使其更具可读性。
使用此代码将使您的按钮在没有选定项目时不执行任何操作。
否则你可以使用 multiline-If 放一个 MsgBox("Please pick an Item")
,来警告用户:
If IsNothing(ListBox1.SelectedItem) Then
MsgBox("Please pick an Item first.")
Exit Sub
End If
// Your Code after that
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
我在尝试处理 ArgumentNullExeption 发生时遇到问题
值不能为 Null
参数名称项目
到目前为止我尝试了以下方法
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)
仍然报错
问题不太清楚,我试试看。
当您的应用程序具有 Listboxes
并使用 .SelectedItem
属性时,您应该始终使用 Try/Catch 或更好地检查用户是否选择了项目:
If IsNothing(ListBox1.SelectedItem) Then Exit Sub
您也可以使用
If ListBox1.SelectedItems.Count = 0 Then Exit Sub
您也可以使用 If/Else,但在我看来,将语句放在代码的第一行使其更具可读性。
使用此代码将使您的按钮在没有选定项目时不执行任何操作。
否则你可以使用 multiline-If 放一个 MsgBox("Please pick an Item")
,来警告用户:
If IsNothing(ListBox1.SelectedItem) Then
MsgBox("Please pick an Item first.")
Exit Sub
End If
// Your Code after that
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