Problems passing focus to TextBox

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a Windows Form Application that has a few TextBoxes, Buttons, and a couple of ListViews. One of the TextBoxes takes an entry (usually by a barcode scanner or the user can type text and hit the TAB character) and then that searches through some
data structures and if found it modifies the color on a row in one of the ListViews and adds a row to the other ListView. What I want to be able to do is to put the focus back on the Textbox that takes the input from the barcode reader.
The problem Im having is that the TextBox doesnt always get focus back after a scan. Sometimes after a scan it looks like the focus is on one of the splitContainers. I can manually take the mouse and put the focus back on the TextBox, but that
would be kind of inconvienient for the user. What I would like would be for the user to be able to scan (which enters text into the textbox and then TABS out). The TextBoxs Leave Event fires and runs the code that manipulates the ListViews and then at the
end the leave event code clears the textbox with TextBox1.Text = "" and then calls TextBox1.Focus()
Ive tried using TextBox1.Select() and/or TextBox1.Focus() and neither one works. I even put in a Apllication.doEvents() so that any pending action that might cause focus to change will happen before I call the Focus() or Select() code.
My guess is that maybe it has to do with some race condition on actions that pull focus away, but how can I keep focus on the text field and still allow the user to use the mouse to press the EXIT or SAVE buttons or even click on an item in one of the ListViews
(which doesnt do anything right now).
I do have a check in the TextBox.leave event that checks for empty values using
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; If TextBox.Text <> <span style="color:#A31515; "" <span style="color:Blue; Then
<span style="color:Green; Do the actions here
<span style="color:Green; Look for text in hashtable
<span style="color:Green; change color on ListViewItem
<span style="color:Green; beep at the user (found it)
<span style="color:Green; Okay setup for next barcode scan

Application.doEvents()
TextBox.Text = <span style="color:#A31515; ""
TextBox.Focus()
TextBox.<span style="color:Blue; Select()
<span style="color:Blue; Else
<span style="color:Green; Dont do anything because the TextBox is empty
<span style="color:Green; This allows the user to leave the textbox and press on other buttons .... etc.
<span style="color:Blue; End <span style="color:Blue; If

[/code]
Anyone have an idea whats going on? I thought for sure that the TextBox1.Focus() or even TextBox1.Select() would work. <hr class="sig Alan Edwards

View the full article
 
Back
Top