美好的一天!
我想从一个条码扫描仪,在文本框中捕获添加一些字符串到列表框,并在加入它,以检查特定的字符串还没有已经添加了。所以我有一个名为txtWO一个文本框捕捉什么样的阅读器扫描,并呼吁lstScanBOM一个列表框,我添加文本框的字符串,如果该项目尚未添加。问题是,无论我做什么,只有在特定的字符串添加两次重复的条目检查开始工作。换句话说,我扫描相同的字符串两次,它增加了它,然后当我扫描了第三次只有它与错误抛出的消息,称这是一个重复。我不明白为什么这样做。代码如下:
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If lstScanBom.Items.Contains(StringToAdd) = True Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'it does not exist, so add it..
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
IMO尝试列出列表框以外的数据。我不明白为什么它不工作,也许我们需要一个第三对眼睛看看吧!?
尝试添加(字符串)的列表作为一个私人的形式中,填充这个作为你的用户扫描,并检查重复的有..
这绝对不是最好的解决办法,但我敢肯定它会帮助!
Private List_Barcodes As List(Of String)
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
List_Barcodes = New List(Of String)
'You can also populate this list on load, if you have a stored cahce of previous scanned barcodes?
'List_Barcodes.Add("0123456")
'List_Barcodes.Add("4567890")
'...etc
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If List_Barcodes.Contains(StringToAdd) Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'Place into dynamic list
List_Barcodes.Add(StringToAdd)
'and Place into your listbox
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
您的条形码阅读器,返回<回车> <换行>的输入。代码捕获输入键(回车= 13),但保留线馈送(10)字符。所以,下次你扫描的东西时它会与换行开始。在实施例中的两个字符串是不同的,因为第一个是“58335001”,第二个是“<换行符> 58335001”。第三个是“<换行符> 58335001”,这是第二个重复。
解决这个问题的方法之一是修剪你的字符串。
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
'Add the .Trim() to remove the leading <line feed> character
barcode = txtWO.Text.Trim()
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
最简单的决定只是为了让你的TextBox控件txtWO不要多而这就足够了!您的代码将正常工作!