Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to read data from bar code scanner to windows form .

i can read data success without any problem

my problem is if i put the button in form it focus on it and not reading data

so that how to put button and remove focus on it this is actually my problem i need to

solve .

C#
public partial class Form1 : Form
    {
        DateTime _lastKeystroke = new DateTime(0);
        List<char> _barcode = new List<char>(10);
        public Form1()
        {
            InitializeComponent();
         
        }

        
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
            if (elapsed.TotalMilliseconds > 100)
                _barcode.Clear();

            // record keystroke & timestamp
            _barcode.Add(e.KeyChar);
            _lastKeystroke = DateTime.Now;

            // process barcode
            if (e.KeyChar == 13 && _barcode.Count > 0)
            {
                string msg = new String(_barcode.ToArray());
                label1.Text = msg;
                //queryData(msg);
                _barcode.Clear();
            }
        }
    }
}


What I have tried:

how to remove focus on button when reading data from scanner barcode
Posted
Updated 25-Feb-17 16:25pm

You never "remove" focus from anything. You have to give the focus to some other control.

Since most barcode scanners are just keyboard emulators (keyboard wedge), everything they scan is just "typed" into whatever control has the focus at the time.

So, typically, you program the barcode scanner to prepend an attention sequence to the barcode. Your form's KeyPreview property has to be enabled so the form code Key events can get the barcode before the destination control does. The forms key event handler watches for the prepended sequence of characters. When it sees the sequence you can move the focus to an appropriate control, usually a textbox, and the rest of the keystrokes coming from the barcode scanner will end up in that control.
 
Share this answer
 
Just focus it in the textbox for the barcode scanner's input. Then on the Form's AcceptButton property, set it to Button1.
 
Share this answer
 
C#
textBox1.Focus();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900