Improve Barcode search in a Textbox C#

前端 未结 5 1035
死守一世寂寞
死守一世寂寞 2021-01-23 21:27

In a WinForm C# application I have a Barcode_textbox. In TextChanged event I have an SQL query to check for the TextBox.Text value in the database which is the barc

相关标签:
5条回答
  • 2021-01-23 21:45

    You could use the Validating event to check the content of the TextBox.
    Your user will be required to press the TAB key to change the current focus from the TextBox to the next control following the taborder and the validating event will be triggered

    private void textBox_BarCode_Validating(object sender, CancelEventArgs e)
    {
         // Code to check the barcode on the SQL database....       
         if(!ValidBarCode(textBox1.Text))
         {
               // Cancel the event and select the text to be corrected by the user.
               e.Cancel = true;
               textBox1.Select(0, textBox1.Text.Length);
         }
    

    }

    Suppose that the next control is the command that executes something using the Barcode inserted (Add)(or a textBox to insert the quantity of the item described by the barcode). After a while all the operation becomes a very natural data entry processing

    0 讨论(0)
  • 2021-01-23 21:57

    I recall how I did this with success.

    I put Timer control in my application with a Interval of a second (1000 milli's). Then I set the Form.KeyPreview property to True.

    In the Form Key Press event I store the key strokes. In the Timer.Tick event check if the length of the recorded key strokes exceeds 12 or so characters.

    Then fire off the call to SQL, once. When you return a record successfully (or if the textbox contains greater than say 20 chars) clear the stored key strokes.


    See update, as at March 2019: https://stackoverflow.com/a/55411255/495455

    0 讨论(0)
  • 2021-01-23 21:59

    Timers are a horrible solution to this.

    Use the KeyUp event of the TextBox and check for a carriage return. Most barcode scanners submit a carriage return after the code.. and if they don't do it by default, they come with barcodes to configure it to do so.

    You can test this by opening Notepad and scanning barcode after barcode into it. Each one will be on a new line.

    0 讨论(0)
  • 2021-01-23 22:05

    What about doing the query async?

    If you can't or want an easy dirty solution, then I would just stick to the timer

    0 讨论(0)
  • 2021-01-23 22:07

    Use timer in this fashion that when user stops typing in your textbox for a given small time say 1 second only then get data from database...

    Also you can place a check at the characters typed by user with a counter such that when it exceeds that minimum(the minimum size that your key can be) get the data...Will save much time

    0 讨论(0)
提交回复
热议问题