[英]Filling a listbox from a textbox without using a button or click event in VB
I'm not sure if it is possible, but I would like to know if a listbox can be updated by adding or removing text by typing that text into a textbox without using a button event or some type of click event. 我不确定是否可以,但是我想知道是否可以通过在不使用按钮事件或某种点击事件的情况下在文本框中键入文本来添加或删除文本来更新列表框。 I've tried using the text_changed event but it inserts the text as I type so I am unable to type an entire string and then move that into a listbox as a whole string.
我尝试使用text_changed事件,但是它会在键入时插入文本,因此我无法键入整个字符串,然后将其作为整个字符串移动到列表框中。
What I am trying to do is scan a magnetic ID card through a reader and have it insert the data from that card into a listbox and when I scan the same card again, it will remove the data. 我要做的是通过读取器扫描磁性ID卡,并将其从该卡中插入到列表框中,当我再次扫描同一张卡时,它将删除数据。 This is for an employee logging system.
这是用于员工记录系统的。
Thanks. 谢谢。
With the following code, if you type a string in TextBox1
and hit Enter, we check to see if the string already exists in ListBox1
. 使用以下代码,如果您在
TextBox1
键入一个字符串并按Enter,我们将检查该字符串是否已存在于ListBox1
。 If so, the string is removed from the ListBox, otherwise it is added. 如果是这样,则从列表框中删除该字符串,否则将其添加。 Then
TextBox1
is cleared. 然后,清除
TextBox1
。
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbCr Then
If ListBox1.Items.Contains(TextBox1.Text) Then
ListBox1.Items.Remove(TextBox1.Text)
Else
ListBox1.Items.Add(TextBox1.Text)
End If
TextBox1.Clear()
e.Handled = True
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.