多次单击列表视图时重置文本框值

时间:2013-10-20 17:05:57

标签: vb.net visual-studio-2010

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

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

1 个答案:

答案 0 :(得分: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