Automatically send barcode scanner input to textbox VB.Net / Winforms - vb.net

I've reviewed dozens of options/solutions on this and I just can't get it to work.
Simply put, I have a VB.Net Winform that has a textbox where a user can manually type in text or they can use a USB connected barcode scanner (that simulates a keyboard) to capture a UPC.
What I'm trying to do is get the barcode input to get entered into the textbox regardless of which control has the current focus.
I have set the KeyPreview property of the form to True.
I then added some code to the frmMain_Keypress event as follows:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
End If
Very simple...and it works, sort of...
If txtSearch already has the focus, the entire barcode/UPC gets entered into the text box.
However, if another control has the focus, every character of the barcode/UPC EXCEPT THE FIRST CHARACTER gets entered into the text box. It always strips off the first character.
I placed some debug statements in the above code to see if the initial character was being read at all and it is being read...just not sent to the text box.
I've seen so many other REALLY complicated solutions to barcode scanning and it seems like I'm really close with something really simple, but, obviously it won't work if it strips the leading character.
Hopefully I'm missing something very obvious.

Change the code in your KeyPress event to:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
txtSearch.Text = e.KeyChar.ToString
txtSearch.SelectionStart = txtSearch.Text.Length
e.Handled = True
End If
That way you capture the first key that comes in.

Related

Why is text clearing from the text field of a combobox if message is displayed?

I'm working in VB.NET, Visual Studio 2017. I have a combobox with DropdownStyle = Dropdown. If the user types something invalid in the text field of the combobox (invalid means it doesn't match a value in the combobox) then we display a message and then return focus to the text field with the text highlighted, so they can see what they typed. The message is displayed from the Validating event.
This works fine if they don't open the dropdown. If they do open the dropdown and, while it is open, they type in an invalid entry, the message displays but the entry they typed clears.
I have put in debug statements to see what events are firing. Before the message displays, I get a DropDownClosed (text is still there), then a TextChanged (text is still there), then a second TextChanged (text is now empty). I think something about losing focus to display the message may be triggering something, but I can't figure out what.
I can save off the text and then replace it after the message displays, but while the message is up, the text field is blank.
Any ideas?
While I still don't understand the series of events that causes the problem, I found a solution. Just before the message is displayed from within the Validating event of the combobox, I set focus to the combobox. I'm guessing that tabbing away from the invalid entry loses focus, and because the dropdown is open, it then closes which somehow clears the entry. Go figure.
Here is the code from the validating event:
If testInList Then
ResultTextBox.Enabled = True
Else
TestComboBox.Focus
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
The "TestComboBox.Focus" line has fixed the issue of the text disappearing. However, if the user's invalid entry is a partial match for an item in the dropdown, then the text in the text field of the combobox is updated to that item, so now it looks like they typed a valid entry but are getting an message that it's invalid. For example, if there is an entry in the dropdown of "NAMC" and they type "NA" (with the dropdown open) and tab away, the entry changes to "NAMC".
Any ideas on how to prevent that?
p.s. The AutocompleteMode is set to None.
So again, I don't understand the sequence of Events that causes the issue, but I've found something that works. If I move the new "TestCombobox.Focus" line outside of the If condition, the text remains in the text field before, during and after the message displays:
TestComboBox.Focus()
testInList = TestEntryValid()
If testInList Then
EnableResultFields(True)
Else
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
This moves the .Focus before TestEntryValid, but I don't see anything in there that would trigger any events:
Private Function TestEntryValid() As Boolean
Dim item As String = TestComboBox.Text
Dim validTest As Boolean = False
If item.Length > 0 Then
Dim index As Integer = TestComboBox.FindStringExact(item)
If index > -1 Then
validTest = True
End If
End If
Return validTest
End Function
If anyone can explain why this works, I'd love to know. If not, thanks to everyone who responded!

How to force match Combobox RowSource row by setting its Text property?

On the shop floor, a virtual (touch) keyboard form with larger buttons is used to enter text into TextBoxes and ComboBoxes.
This works fine, and combobox text is set correctly, however the RowSource is not matched as it would be when you type directly into the ComboBox with a physical keyboard. The entire list is displayed as if you had just pressed the dropdown button without typing a character.
In the example below, there is a Stefan in the list, but that row is not looked up.
I've tried SetFocus, Requery, Refresh, Dirty, and calling _AfterUpdate, in combinations and with DoEvents, to no avail.
I've even tried to select, Cut, and Paste the text (but even setting SelStart and SelLength to correct values does not select it, so I'm assuming it cuts and pastes a range of zero characters). If I could make the text-selection work, I could probably get this to work.
Dim ctrlPrevious As Control
Set ctrlPrevious = Screen.PreviousControl
ctrlPrevious.SetFocus
ctrlPrevious.text = sTemp
ctrlPrevious.SelStart = 0
ctrlPrevious.SelLength = Len(sTemp)
ctrlPrevious.Cut
ctrlPrevious.Paste
Is there a way to force the AutoComplete behavior?
Use SendKeys to mimic the normal keyboard beheviour instead of all the above code.
So in your btnPressed_Clicked event
Dim ctrlPrevious As Control
Set ctrlPrevious = Screen.PreviousControl
ctrlPrevious.SetFocus
SendKeys btnPressed.caption

VB.NET add text to the front of text

I am trying to add text to current text that is in a textbox using a checkbox. Once the checkbox is checked, it will "add" the text to the textbox before the text already in the textbox.
example:
if the textbox said "Jake", it would say "Hello Jake" once the checkbox was checked.
EDIT: sorry for the quick question. I'm in a hurry. But the only method I could think of was concatenating and appending text. As far as I know append() adds only to the end, and concatening isn't the logical approach. I don't have a coded example because I dont even know how to approach this issue. thanks.
It's getting a thumbs down because its so simple, but not. I'm using multiple checkboxes. So one needs to be in the very front, one in the center, etc. Each checkbox injects text into the textbox a certain way. I can do this with nested if statements, but then we got a mess.
Try using this code :
if chbHello.Checked then
txtName.Text = chbHello.Text + " " + txtName.Text
Note : if the checkbox is the trigger put this code in the checkbox.
All it does is see if the checkbox was checked or not.
When its checked just concatenate the text of the textbox and the checkbox with the
checkbox first.

Visual Basic Help, format check

When I enter a non-numeric value into a numeric field, vb auto validates the field and the system does not allow me to proceed to fill the next text box.
How can I work around this? I want to write code to perform the validation on the numeric.
This is what happened as shown in picture. I cannot continue to input other fields.
field
There is probably an event handler that is limiting the input to numeric fields and possibly setting the focus on the textbox. This could be in Keydown, Leave, TextChanged, or some other events. You can replace this validation code with your own.

MS Word ActiveX Textbox Spell Check

I am working on a MS Word form for a client where they want the ability to count the number of words, check spelling, have a character limit, and have the rest of the form locked down so that the end user cannot change anything they are not supposed to. I have attempted to convince them that word count and character limit are redundant if we have proper instructions, however, this is what they want. They also want the form to be able to "work" even if the user does not enable macros, meaning, they want it locked and a character limit first and foremost.
I know that if we just rich text content controls and put the form into a group spell check and word count word while also "locking" the remainder of the form, except user content controls do not allow for character limits and using legacy/activex controls in a grouped form locks those controls as well.
So, for now, I have settled on using the ActiveX Textbox (this is negotiable if I have a reason to use the legacy textbox) and have achieved the minimum "workability" (if that's even a word). The only way I have figured out to check the spelling is below:
Sub chkSpelling()
Activedocument.Tables(1).Rows(26).Cells(1).Range.Text = txtRole.Text
Activedocument.Tables(1).Rows(26).Cells(1).Range.checkSpelling
txtRole.Text = Activedocument.Tables(1).Rows(26).Cells(1).Range.Text
'... so on and so forth throughout each text box
End Sub
The issue is that this is not good enough for my standards. Printing the text of each textbox (up to 1700 characters) at the bottom of the screen each time I need to check the Spelling is unacceptable. Does anyone else have any ideas?
Thank you for your assistance.
Answer with help from #bibadia
Dim doc As Document
Set doc = Documents.Add(, , wdNewBlankDocument, False)
doc.Paragraphs(1).Range.Text = txtRole.Text
doc.Paragraphs(1).Range.CheckSpelling
txtRole.Text = Replace(doc.Paragraphs(1).Range.Text, Chr(13), "")
Use CreateObject to create a new word instance
Ensure .Visible = False (I think that is the default)
Create a new document in that instance.
Copy the text to be checked into that
Spell check.
Remove the document and the Word instance.
If I understand your question correctly then,
I need to check the Spelling is unacceptable. Does anyone else have any ideas?
If text boxes are named with numbers, you might be able to use loop, and save your coding lines with time.
want the form to be able to "work" even if the user does not enable macros
If user did not enable macros, he would not be able to see form (is it User Form, you want to say) and no background macro code would run. (Tested on Ms-Excel 2007), ultimately failing all.
regarding text box control on user form
yes that can be set to character limit from properties menu, and spell check can be done using your method.
Set range2 = Documents("MyDocument.doc").Sections(2).Range
range2.CheckSpelling IgnoreUpperCase:=False, _
CustomDictionary:="MyWork.Dic", _
CustomDictionary2:="MyTechnical.Dic"