0

我为我正在处理的一个小型应用程序创建了一些代码,基本上有一个列表视图框,当单击特定行时,一些详细信息将导入到下面的文本框中。它工作正常,数据按要求输入,但如果我在不同的行上再次单击它,例如第一次单击的信息仍保留在框中,它不会被新值覆盖。

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    For i As Integer = 1 To 2
        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 textbox1 As TextBox = DirectCast(matches(0), TextBox)
            If TextBox1.Text.Trim.Length = 0 Then
                TextBox1.Text = ListView1.SelectedItems(0).SubItems(2).Text
                Exit Sub
            End If
        End If
    Next
End Sub
4

1 回答 1

1

我会像这样修改代码:

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
For i As Integer = 1 To 2
    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 textbox1 As TextBox = DirectCast(matches(0), TextBox)
        If TextBox1.Text.Trim.Length = 0 Then
            TextBox1.Text = ListView1.SelectedItems(0).SubItems(2).Text
            Exit Sub
        Else
            Textbox1.Text = ""
            TextBox1.Text = ListView1.SelectedItems(0).SubItems(2).Text
            Exit Sub
        End If
    End If
Next
End Sub
于 2013-10-20T17:09:14.463 回答