Solved

Force focus in a windows form

Posted on 2011-02-18
5
536 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

8 Experts available now in Live!

Get 1:1 Help Now