Solved

Force focus in a windows form

Posted on 2011-02-18
5
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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