C# method getting invoked twice on Enter Key

Viewed 315

I am in the process of creating a POS system using C# and I have an issue with Textbox to read bar-code.

I want to read bar-code from the scanner either when the textbox has focus or not. Again I want the textbox to have capability to search for product manually that is user input the bar-code and then press the enter key the bar code captured should be used to query MySQL.

I have used this code to read bar-code from scanner when my textbox has focus or not but the problem is that when I want to search for product manually using enter key the method to query db is getting invoked twice and I don't understand why. Need help to prevent the function from getting invoked twice as well as provide functionality to search bar-code on enter and when textbox has no focus that is get input from scanner

 private string _barcode = "";
 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)//read when no focus
 {
            char c = (char)keyData;

            if (char.IsNumber(c))
                _barcode += c;

            if (c == (char)Keys.Return)
            {
                
                   read_scanner(_barcode);
                    _barcode = "";
                
                 
            }

            return base.ProcessCmdKey(ref msg, keyData);
 }

This my code to search manually

private void txtbarcode_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Enter)
   {
      // onQuery = true;
      read_scanner(txtbarcode.Text);
   }
}

And here is my method which is getting invoked twice when I press enter key

Product product;
public void read_scanner(string barcode)//this method is getting invoked twice when i search product manually
{
           // 
           
            product = HibernateDao.getRecord("from Product u where u.barcode='"+barcoded+"'");
            if (product != null)
            {
                
                pos_datagrid.Rows.Add(barcode, product.product_name, product.product_desc, null, "1", null, product_Cost);
                
            }
            else
            {
               AppUtil.showErrorBox("Invalid bar code!!");
                
            }

           
}
2 Answers

The source of your problem is unclear. Although if you want to track the source of what triggers your method, use System.Environment.StackTrace and output that as a debug message. You might have to use the "Debug" configuration for that.

The ProcessCmdKey is a virtual method on all Forms that listens to all keys and return true if it was a command key and false for other keys, so you might not need to implement KeyDown at all.

documentation here

I've changed the code a little to make use of a StringBuilder and add the values to that StringBuilder.

StringBuilder _barcodeBuilder = new StringBuilder();
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{

    if (keyData != Keys.Return)
    {
        switch (keyData)
        {
            case Keys.NumPad0:
                _barcodeBuilder.Append(0);
                break;
            case Keys.NumPad1:
                _barcodeBuilder.Append(1);
                break;
            case Keys.NumPad2:
                _barcodeBuilder.Append(2);
                break;
            case Keys.NumPad3:
                _barcodeBuilder.Append(3);
                break;
            //and so on for the rest of the numpad keys
            default:
                _barcodeBuilder.Append((char)keyData);
                break;
        }
    }
    else
    {
        MessageBox.Show(this, _barcodeBuilder.ToString());
        //read_scanner(_barcodeBuilder.ToString());
        textBox1.Clear();
        _barcodeBuilder = new StringBuilder();
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Numpad keys are a bit strange, so you can handle them with a switch statement.

Related