I have written a small application in c# which collects scanned barcodes. First scanned barcode goes into a txtbox, then I write them into a txt file. So the application window has to be active all the time. Sometimes if another window pops up, my application becomes inactive and the scanned barcodes are lost. Is there any way I can make my application window to be active all the time.
Asked
Active
Viewed 587 times
1
-
You could declare a variable in your form and save the barcodes in it, you know? – Nolonar Jun 04 '13 at 14:04
-
For always on top, check out:http://stackoverflow.com/questions/278237/keep-window-on-top-and-steal-focus-in-winforms – George Johnston Jun 04 '13 at 14:04
-
I dont have any problem saving the barcode if the window is active all the time. I just want my window to get focus again – user2007860 Jun 04 '13 at 14:05
-
I have done void MainFormDeactivate(object sender, EventArgs e) { this.Activate(); this.TopMost = true;} this didnt work – user2007860 Jun 04 '13 at 14:06
-
If the other window is part of your application, you can always do `form1.Focus();` assuming the other form knows about `Form1 form1`. If the other window is not part of your application, then I'm afraid it's not possible to regain focus. – Nolonar Jun 04 '13 at 14:08
-
That is bad. The other window can be anything, like any other windows application window – user2007860 Jun 04 '13 at 14:14
-
Yeah, you are trying to hijack the operating system. There are reasons why you can't do this. If another window pops up, its probably because someone clicked it? If your users don't know how to use the software, you need user training, don't waste time trying to get that last feature working, when it goes against normal computer user interaction. – David C Jun 04 '13 at 14:22
-
but the computer will be only used for barcode scanner application, so nobody else will use it. I can change windows setting so that no other application pops up, I guess. – user2007860 Jun 04 '13 at 14:25
-
You could try grabbing the barcode at a lower level: [Distinguishing Barcode Scanners from the Keyboard in WinForms](http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/) – Idle_Mind Jun 04 '13 at 19:35
-
Idle_mind, yes I had seen that article I have tried to undestand it but couldnt figure out. I will spend some more time to understand it. Yesterday I was not at work. Whole one day scan was lost, since some other application has popped up and my application was out of focused. I have to understand this article better – user2007860 Jun 21 '13 at 15:20
1 Answers
0
You could use a timer that constantly brings the form to the front and activates it, while not perfect, it should get the job done.
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 50;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
this.BringToFront();
this.Activate();
}

John Koerner
- 37,428
- 8
- 84
- 134
-
Thanks all for all your responses. I have tried John you suggested. The form always stays on top but it doesn't really stay focused.(Sorry for late response) – user2007860 Jun 21 '13 at 15:19