locked
C#.Net 2008 Form KeyDown event and TextBox KeyDown Event RRS feed

  • Question

  • Hi Friends,
    Encounter another interesting problem of practical event. I have used FORM KEYDOWN event to navigate the cursor and on the Primary Key TextBox I tried to validate the input after ENTER was pressed it does not work.

     private void FrmCustomerRef_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }

                //navigate textbox
                if (e.KeyCode == Keys.Enter)
                {
                    ProcessTabKey(true);
                }
            }

    --------------------------------------------------
    Primay key TextBox

    private void txtCustID_Enter(object sender, EventArgs e)
      {
      //check for duplicate CustomerID
              
      int intReturn = 0;   
      try
         {
           string strSql = "Select * from Customers Where CustomerID  = '" + this.txtCustID.Text + "'";
           sqlconn = new SqlConnection(connstr);
           sqlcmd = new SqlCommand(strSql, sqlconn);
           sqlconn.Open();

           intReturn = Convert.ToInt32(sqlcmd.ExecuteScalar());

           if (intReturn > 0)  // record duplicate
             {
                string txtMsg = "This CustomerID : " + txtCustID.Text + "\n"
                                  + " belongs to another Customer ";
               MessageBox.Show(txtMsg, "Duplicate CustomerID", MessageBoxButtons.OK);
             }
      }
      catch (Exception Ex)
         {
             MessageBox.Show(Ex.Message);
          }
     
     }

    This event txtCustID_Enter(object sender, EventArgs e) was not activated

     

    • Moved by Eric Smyth - MSFT Tuesday, April 27, 2010 4:54 PM Wrong Forum (From:Transactions Programming)
    Tuesday, April 27, 2010 7:15 AM

Answers