Solved

Force focus in a windows form

Posted on 2011-02-18
5
543 Views
Last Modified: 2012-05-11
There are a windows form with some controls: datagridview, textboxes, labels, buttons. Operator use a scanner configurated in keyword emulator to read barcodes. If the focus of the cursor is not in the right place (dedicated textbox) the content is not processed and in some cases can cause errors. Fortunately barcode starts with a particular character ("*") so I've think to intercept * and force focus to the textbox but I don't know witch event can help me.
0
Comment
Question by:fantamen
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 34926057
fantamen,

Can you be more specific as to how your windows Form is called and how your application is designed.  It seems to me that you would force the focus to the proper input field prior to the scanning, as a part of the application design.  Instead, it seems as though you wish to force focus as a result of a barcode being scanned.

A step by step explanation of your process would help to understand your problem.

Regards,

Keith
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 34926288
the form has a KeyPreview property. set it to true.

now handle the KeyPress event of the form. If you find your *, do a YourTextBoxControlName.Focus
0
 

Author Comment

by:fantamen
ID: 34926344
For example a barcode can be this: *045 02341
Keyboard emulator scanner read this code. If the active control of the form is txtBarCode all is ok. If operator has put the focus to a cell in a datagridview in the form and then read the barcode naturally *045 02341 is written in the cell and this is the problem. If I can intercept the first character of the barcode (*) and in presence of * force the focus to txtBarCode i will solve my problem.


0
 
LVL 10

Expert Comment

by:kdwood
ID: 34926381

So it seems that your form is not just used for scanning input, but has other interactive purposes.  In that case, I believe emoreau's solution should work.

Regards,

Keith
0
 

Author Comment

by:fantamen
ID: 34926904
Dear Emoreau and kdwood your suggestion solve my problem. Very good!
I've add two lines in then Form_KeyPress event, after txtBarCode.Focus, one to add at the beginning of the code the * and another to set the selection start to the second position in the text box.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 32
Get list of word ducuments in a folder 10 36
Help with sorting data in Listbox using VB.NET 3 53
Get Client IP on RDS - VB.NET 15 27
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now