1

I'm developing a simple program that reads a card or a barcode and logs that on a database. Before, I developed the same application in Visual Basic and it was working great, but due to a lot of changes on our servers, we decided to develop this app in C#.

I can get my application to start with Windows putting its shortcut on 'startup' of the start menu but the problem is that its not getting focus so that the cards and barcodes can be read and that way my program is simply useless. The machines we use are running Windows XP and Windows 7.

How is the best way to start my application on Windows Startup and keep the focus in it?

  • 1
    Please explain why the application needs to keep focus to function properly. – Emond Jul 26 '11 at 18:51
  • The app needs focus because most barcode/card scanners act as keyboards. When you scan something, the decoded data gets typed wherever the cursor/focus happens to be. – Tim Coker Jul 26 '11 at 18:58
  • This is worth a read: http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx – n8wrl Jul 26 '11 at 19:19

3 Answers3

3

This is, in general, a pretty bad design. I just finished a project involving barcode readers and set them up to act as serial ports, instead of keyboards. You should check whether this is an option with your hardware, as the end result will be much more reliable.

That being said, you can create a timer in your form that executes this.Focus() and this.BringToFront() to steal focus. Be aware that this will, by default, only cause the task bar to flash. You'll need to use TweakUI to enable focus stealing.

Another option is discussed here on SO. Very similar question, actually. Basically, you hook the keyboard input at a low level.

Community
  • 1
  • 1
Tim Coker
  • 6,484
  • 2
  • 31
  • 62
0

On Form Activated Event call This.SentToBack()

zafar shakeel
  • 61
  • 1
  • 5
0

Please try and find another way, monitor system events or use a polling mechanism.

Stealing focus should be avoided, read this to understand why

If I had a dollar for every time I typed a password, cleartext in the wrong application because it stole focus...

Emond
  • 50,210
  • 11
  • 84
  • 115