1

I've created a stock locater/mover program. The scenario sequence is as follows:

  • First, the user will scan a QR code into textBox1 and then scan a location into textBox2.
  • Next, an update statement will execute resulting in 'moving' the stock's location.

How do I auto move the cursor into textBox2 after textBox1 has been populated with a QR Code?

Please note the QR codes vary in length. This prevents me from using textBox max length. I've currently tried the following:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //part number textbox
            var partNumber = textBox1.Text;
            partNumber = partNumber.TrimEnd('\r', '\n');

            if (textBox1.Text!=null)
            {
                textBox1.Select();
            }
            else
            {
                textBox2.Select();
            }
        }

Using the aforementioned code, the first character of the QR code is input into textBox1 and the remaining characters are input into textBox2. The desire is to have all the QR code characters in textBox1 and then have the cursor change focus to textBox2.

HappyCoding
  • 641
  • 16
  • 36
Will Howard
  • 45
  • 1
  • 10
  • Most scanners have an option to append a carriage return|+line feed to the end of the read value to deal with this requirement, see if your does. – Alex K. Apr 07 '17 at 14:19
  • 1
    @Gudgip that question has nothing whatsoever to do with this question. – Alex K. Apr 07 '17 at 14:20
  • 1
    FYI, `textBox1.Text` will never be `null` in the `TextChanged` event. And if it was, your call to `.TrimEnd` would throw a null reference exception. – Rufus L Apr 07 '17 at 14:20
  • So before the user scans the QR code there is no text in textBox1, once the user scans the QR code then there is text. I need to identify this and move the cursor to textBox2 if textBox1 is not null. – Will Howard Apr 07 '17 at 14:30
  • @AlexK. I've had to trim the end of the scanner's input so that it works with the regex I've implemented. I'd rather not have to go back and edit the regex as it took an age to get right due to the QR codes having two string formats. – Will Howard Apr 07 '17 at 14:32
  • 1
    You can add a string to the end of the QR and in the changed event you can check if the string apears in the QR – itay_421 Apr 07 '17 at 14:35
  • @itay_421 the user will use a barcode scanner and scan the QR code which acts a keyboard input into the various text boxes – Will Howard Apr 07 '17 at 14:38
  • if the scanner output ends with \r\n just chop it off and carry on? A fixed known terminating delimiter from the hardware is the correct way to do this – Alex K. Apr 07 '17 at 14:40
  • The regex isn't applied until the user clicks the 'update' which updates the products stock location in the DB. I can't add anything to the end of the QR code as this wouldn't be accounted for by the regex and break stuff. – Will Howard Apr 07 '17 at 14:43
  • 1
    Looks like you are throwing away exactly the character that you need to detect that the first field is completely entered. Use the KeyDown event to detect it, use e.Handled = e.SuppressKeyPress = true to prevent it from getting added to the textbox. – Hans Passant Apr 07 '17 at 14:45
  • @HansPassant I think you're right, i've edited the regex and removed the trim so I can use the barcode scanners next line function. Will let you all know how it goes. – Will Howard Apr 07 '17 at 14:50
  • @HansPassant thanks for the assistance solved it – Will Howard Apr 07 '17 at 15:03

2 Answers2

1

Solution;

        private void textBox1_KeyPress(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox2.Select();
        }
    }

then went to the properties of textBox1 and set the 'KeyDown' to textBox1_KeyPress under 'Events' and it works.

Will Howard
  • 45
  • 1
  • 10
0

A similar issue has been described in another question here on SE.

The following answer, posted by esskar might be of interest to you. Since the scanner will probably take only an instant to scan the QR code into the TextBox, you might use the technique in that answer to start a timer when TextChanged fires. I'd set the interval of the Timer at around 500ms, that should be enough, and once it fires you can be pretty sure that the QR code is inside the TextBox.

Obviously this isn't a perfect solution, since in some cases the scanner might lag or simply be unable to deliver the QR code in that time-frame for whatever reason.

You will need to implement a check for common QR code length and validate the code before you go on.

To be clear: There's no need to create a new type of TextBox. Just start a Timer when TextChanged is fired.

Community
  • 1
  • 1
r41n
  • 908
  • 7
  • 18
  • 1
    Thanks for this suggestions, I'll take a look :) – Will Howard Apr 07 '17 at 14:38
  • @WillHoward, to make it clear, I didn't suggest you to create a new TextBox implementation as suggested in that answer. Rather you only need to create a Timer instance with an interval of 500ms once the TextChange event is fired. – r41n Apr 07 '17 at 14:45