简体   繁体   English

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

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

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

If you want to check if a subitem is null or empty, you could add something like this line of code:如果要检查子项是否为空或为空,可以添加如下代码行:

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希望这有帮助

Try to add some validation to ListView1.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM