美好的一天!
我想将条形码扫描器中的一些字符串(在文本框中捕获)添加到列表框中,然后在添加之前检查特定字符串是否尚未添加。所以我有一个名为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
答案 0 :(得分:0)
IMO尝试列出ListBox之外的数据。我不明白为什么它不起作用,也许我们需要第三双眼睛才能看到它!?
尝试在表单中添加一个(字符串)列表作为私有,在用户扫描时填充此列表,然后在那里检查副本..
这绝对不是最好的解决方案,但我相信它会有所帮助!
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
答案 1 :(得分:0)
最简单的决定是仅使textBox控件txtWO不能多行 这就足够了!您的代码将正常工作!