Reading from scanner into textbox and retrieving data based on code from textbox RRS feed

  • Question

  • Hi All.

    I am reading a bar code via scanner into a textbox, I am trying to get the Item price from database by code from textbox. My problem is that i dont know how to do so..!

    I have tried this code but it doesn't work, it doesn't get anything. It is a windows forms application with c#

    private void txtCode_KeyPress(object sender, KeyPressEventArgs e)

                 string code = txtCode.Text;

                 decimal price = Item.getPriceByCode(code);

                 txtTotalDue.Text = price.ToString();


    Tuesday, March 8, 2016 12:00 PM


All replies

  • A barcode scanner is usually an input device nowadays.

    They work just like a keyboard.

    If you focus a textbox and type on a keyboard then the letters appear in that textbox.

    If you focus a textbox and scan a barcode then the numbers appear in that textbox.

    You could try handling the textbox's textchanged event.


    In there you could check the number of characters in the textbox and if it's at least the length of a barcode you could go get the price.

    private void TB_TextChanged(object sender, EventArgs e)
      if (TB.Text.Length >10 )
        // Get the price

    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, March 8, 2016 1:53 PM
  • @Andy ONeill: thank you for your reply.

    The digits may vary from code to code, beside, some items has no barcode lable, and i have to enter a manual code.

    I found that i had a code that did throw and exception in the getPriceByCode(code) method, i had to remove some code.

    I am now facing another problem.  I am still working and if i can't find a solution i will make a new question about that.

    again thank you for your help..

    Tuesday, March 8, 2016 8:48 PM
  • Hi Kamiran,

    As a supplement, I suggest you use ‘Regular Expressions’ with 'TextChanged' event.

            private void textBox_TextChanged(object sender, EventArgs e)
                string pattern = "your pattern";
                if (System.Text.RegularExpressions.Regex.IsMatch((sender as TextBox).Text, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    //do something

    Regex Class

    How to: Search Strings Using Regular Expressions (C# Programming Guide)



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 9, 2016 8:36 AM