473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

KeyDown Event

126 New Member
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
2 2812
GaryTexmo
1,501 Recognized Expert Top Contributor
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
449 Recognized Expert Contributor
@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.KeyPres s event and there execute your function
Apr 28 '10 #3

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

Similar topics

3
9233
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I click on a row (withs will turn blue then) and then click on it again . Now if I press any key the event will fire (except for the arrow keys). I also tried to override the IsInputKey => no luck. I also tried to override the ProcessCmdKey => With...
4
13121
by: Tony | last post by:
I'm building a simple "breakout" style video-game, and I want keyboard control of the paddle. What I need is for an event handler to respond whenever a key is pressed, regardless of what the focus is set to. So far, the best I've been able to accomplish is to create a button and have that button handle the KeyDown event. I've tried using KeyDown on the form, but it simply doesn't work. I did a search on this, and found on site that...
4
8637
by: Anne | last post by:
hie again, i have 3 textbox and i would like the user to go to the next textbox by pressing the 'ENTER' key. i have tried using this: Private Sub txtRequestor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRequestor.KeyDown If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
4
7130
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles ListBox1.KeyUp, ListBox1.KeyDown If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue = Keys.End Or e.KeyValue = Keys.Home Then
1
3309
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I add a keydown event handler for some control on the form, say a textbox ... Private Sub txtBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBox.KeyDown) and then give that control focus the keydown...
0
1757
by: tony | last post by:
Hello!! I have a derived class called StringClassEditor which inherit from UITypeEditor listed below. Now to my question in method EditValue in this class I have this statement lb.KeyDown += new KeyEventHandler(ListBox_KeyDown); I have set a breakpoint in the event handler method ListBox_KeyDown but this method is never called. So this event KeyDown for control ListBox is never occuring.
3
5252
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the handler does not see the combination. Now this control is contained within another control which is contained within another. The top most control does see the CTRL+Z. I can easily pass down the key info, but why does the nested control see...
3
4302
by: win | last post by:
when the cursor is in a textbox, only coding in the keydown event of the textbox triggered, the coding in the keydown event of the form does not triggered! Problem: I need to change a VB6 program to .Net. It uses function key(e.g. F12) to close the form, now i need to write the coding in the keydown event of all controls form. Can anyone help me? Thanks a lot.
2
19295
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I have created for this purpose is derived from class UserControl.
14
2972
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.