Aung Kaung Hein
Aung Kaung Hein

Reputation: 1566

How to stay focused on Textbox when Barcode is scanned in Windows Forms Application?

I have a Snap 3100 series USB Linear Barcode Scanner Gun. It works as keyboard wedge by capturing data from barcode image and returns the value.

I wrote the program to auto-focus and display the scanned value in Textbox. But it loses its focus when users do other functions and they have to put the cursor back to Textbox to get the value. This is POS System and I don't want the operators to use a mouse when focus is lost.

I tried using both:

textbox1.Focus(); and

this.ActiveControl = textbox1;

but not working.

Any help will be very much appreciated.

NOTE: In web application, I would use JQuery to handle this problem. I need something like this in Winforms.

$(function() {
 // Focus on load
 $('.scanner').focus();
 // Force focus
 $('.scanner').focusout(function(){
     $('.scanner').focus();
 });
 // Ajax Stuff
 $('.scanner').change(function() {
     $.ajax({
         async: true,
         cache: false,
         type: 'post',
         url: '/echo/html/',
         data: {
             html: '<p>This is your object successfully loaded here.</p>'
         },
         dataType: 'html',
         beforeSend: function() {
             window.alert('Scanning code');
         },
         success: function(data) {
             window.alert('Success');
             $('.objectWrapper').append(data);
         },
         // Focus
         complete: function() {
             $('.scanner').val('').focus();
         }
    });
});
});

Upvotes: 0

Views: 5610

Answers (1)

Louis van Tonder
Louis van Tonder

Reputation: 3700

form Catch keypress events on the keyboard. A barcode scanner sends a return character (enters) after the scanned code. Catch the whole thing, decide if its a barcode, and enter it into the required textbox?

http://msdn.microsoft.com/en-us/library/ms171538%28v=vs.110%29.aspx

Upvotes: 2

Related Questions