我不完全理解第四步,但看看下面的代码
Dim ListItems1 As New List(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
Label1.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListItems1.Add(TextBox1.Text)
ListView1.Items.Add(TextBox1.Text)
Label1.Text = ""
TextBox1.Text = ""
TextBox1.Focus()
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 0 Then
Button1.Enabled = False
Else
If ListItems1.Contains(TextBox1.Text) Then
Label1.Text = "Directory is already in the listview"
Button1.Enabled = False
Else
Label1.Text = "Directory is not in the listview"
Button1.Enabled = True
End If
End If
End Sub
如果您不希望按钮存在,请尝试以下操作
Dim ListItems1 As New List(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label1.Text = ""
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 0 Then
Label1.Text = ""
Else
If ListItems1.Contains(TextBox1.Text) Then
Label1.Text = "Directory is already in the listview"
Else
Label1.Text = "Directory is not in the listview"
End If
End If
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 And ListItems1.Contains(TextBox1.Text) = False And TextBox1.Text.Length > 0 Then
ListItems1.Add(TextBox1.Text)
ListView1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
Label1.Text = ""
End If
End Sub