vb.net编写一个为列表框添加删除选项的应用程序,如果该字符串在列表框中,

2024年11月15日 02:20
有2个网友回答
网友(1):

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.ToString() = Chr(13) Then
Dim str As String = TextBox1.Text
If Not ListBox1.Items.Contains(str) Then
ListBox1.Items.Add(str)
End If
End If
End Sub

Private Sub btn_Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex > 0 Then
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End If
End Sub

Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
End Sub

网友(2):

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.ToString() = Chr(13) Then
Dim str As String = TextBox1.Text
If Not ListBox1.Items.Contains(str) Then
ListBox1.Items.Add(str)
End If
End If
End Sub