By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,132 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,132 IT Pros & Developers. It's quick & easy.

KeyDown Event

100+
P: 126
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

Expand|Select|Wrap|Line Numbers
  1. private void FrmCustomerRef_KeyDown(object sender, KeyEventArgs e)
  2.         {
  3.             if (e.KeyCode == Keys.Escape)
  4.             {
  5.                 this.Close();
  6.             }
  7.  
  8.             //navigate textbox
  9.             if (e.KeyCode == Keys.Enter)
  10.             {
  11.                 ProcessTabKey(true);
  12.             }
  13.         } 

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

Expand|Select|Wrap|Line Numbers
  1. private void txtCustID_Enter(object sender, EventArgs e)
  2.   {
  3.    //check for duplicate CustomerID
  4.  
  5.   int intReturn = 0;   
  6.   try
  7.   {
  8.   string strSql = "Select * from Customers Where CustomerID  = '" + this.txtCustID.Text + "'";
  9.   sqlconn = new SqlConnection(connstr);
  10.   sqlcmd = new SqlCommand(strSql, sqlconn);
  11.  sqlconn.Open();
  12.  
  13.   intReturn = Convert.ToInt32(sqlcmd.ExecuteScalar());
  14.  
  15.   if (intReturn > 0)  // record duplicate
  16.      {
  17.   string txtMsg = "This CustomerID : " + txtCustID.Text + "\n"
  18.                                           + " belongs to another Customer ";
  19.      MessageBox.Show(txtMsg, "Duplicate CustomerID", MessageBoxButtons.OK);
  20.            }
  21.        }
  22.    catch (Exception Ex)
  23.      {
  24.               MessageBox.Show(Ex.Message);
  25.       } 
  26.   } 
Apr 27 '10 #1

✓ answered by ThatThatGuy

@lenniekuah
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.KeyPress event and there execute your function

Share this Question
Share on Google+
2 Replies

GaryTexmo
Expert 100+
P: 1,501
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!
Apr 27 '10 #2

ThatThatGuy
Expert 100+
P: 449
@lenniekuah
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.KeyPress event and there execute your function
Apr 28 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.