vb.net 如何检查 ListView SubItem 中的空项或空项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16713941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:42:39  来源:igfitidea点击:

How do I check for a null or empty item in a ListView SubItem?

vb.netlistview

提问by 85l00k85

I have created this listview box that when contents are double clicked the values are inserted into text boxes. There are spaces in some items and a couple have null values in the subitems. When these particular list items are clicked it crashes the software. Is there a way I can check if a subitem is null or empty?

我创建了这个列表视图框,当双击内容时,这些值会插入到文本框中。某些项目中有空格,并且有几个子项目中有空值。当单击这些特定的列表项时,它会使软件崩溃。有没有办法可以检查子项是空还是空?

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    For i As Integer = 1 To 7
        Dim tbName As String = "TextBox" & i
        Dim matches() As Control = Me.Controls.Find(tbName, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
            Dim tb As TextBox = DirectCast(matches(0), TextBox)
            If tb.Text.Trim.Length = 0 Then
                tb.Text = ListView1.SelectedItems(0).SubItems(0).Text
                Exit Sub
            End If
        End If
    Next

回答by davidsbro

If you want to check if a subitem is null or empty, you could add something like this line of code:

如果要检查子项是否为 null 或为空,可以添加类似以下代码行的内容:

If tb.Text.Trim.Length = 0 Then
      'check if subitem isn't null and its text is not ""
       If Not IsNothing(ListView1.SelectedItems(0).SubItems(0)) AndAlso ListView1.SelectedItems(0).SubItems(0).Text <> "" Then
            tb.Text = ListView1.SelectedItems(0).SubItems(0).Text
            Exit Sub
       End if
End If

Hope this is helpful

希望这有帮助

回答by Carlos Landeras

Try to add some validation toListView1.SelectedItems(0).SubItems(0).Text

尝试添加一些验证ListView1.SelectedItems(0).SubItems(0).Text

It seems to be null sometimes:

有时似乎为空:

Use this validation:

使用此验证:

If Not ListView1.SelectedItems is Nothing
   AndAlso Not ListView1.SelectedItems(0) is Nothing
   AndAlso Not string.IsNullOrEmpty(ListView1.SelectedItems(0).SubItems(0).Text)  Then

                tb.Text = ListView1.SelectedItems(0).SubItems(0).Text
End if

Full code:

完整代码:

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    For i As Integer = 1 To 7
        Dim tbName As String = "TextBox" & i
        Dim matches() As Control = Me.Controls.Find(tbName, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
            Dim tb As TextBox = DirectCast(matches(0), TextBox)
            If tb.Text.Trim.Length = 0 Then
              If Not ListView1.SelectedItems is Nothing
                     AndAlso Not ListView1.SelectedItems(0) is Nothing
                     AndAlso Not string.IsNullOrEmpty(ListView1.SelectedItems(0).SubItems(0).Text)  Then
                tb.Text = ListView1.SelectedItems(0).SubItems(0).Text
              End if
                Exit Sub
            End If
        End If
    Next