KeyDown Event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    KeyDown Event

    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.

    Here are the coding

    Code:
    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

    Code:
    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);
          } 
      }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Oh, I think it's because you've got a key down event for the form trying to control a text box. I believe these work for the control that's in focus, so if you're typing in a textbox and press enter, it won't fire the event in the form's key down event.

    Try a key down event for the text box :)

    Also, there is a series of events for "Validating " the textbox which happen automatically when the textbox would otherwise consider it's input complete (ie, enter or leave focus) which you can look into.

    Hope that helps!

    Comment

    • ThatThatGuy
      Recognized Expert Contributor
      • Jul 2009
      • 453

      #3
      Originally posted by lenniekuah
      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.

      Here are the coding

      Code:
      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

      Code:
      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);
            } 
        }
      TextBox.Enter doesn't stand for the event to fire when enter key was pressed...

      it actually fires when the textbox has received focus...
      you can check for a Enter key on TextBox.KeyPres s event and there execute your function

      Comment

      Working...