Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Catch enter key
Message
From
01/10/2005 20:05:29
 
 
To
28/09/2005 12:02:34
General information
Forum:
ASP.NET
Category:
Forms
Title:
Environment versions
Environment:
VB.NET 1.1
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
01053288
Message ID:
01055291
Views:
21
Wow, Juan ... I'm surprised no one has come up with a good answer to this yet ... I've been out at the Summit for the past few days, otherwise I would've answered sooner (actually, I'm still here at the Summit <g>).

Basically, you have to override the ProcessCmdKey method in your DataGrid sub-class ... see this article for more information:

http://www.codeproject.com/cs/database/DatabaseAcessWithAdoNet1.asp#Keystrokes

Here's an excerpt from the article, along with the code:

9. How to trap keystrokes (Up, Down, Esc, NumLock...) in the DataGrid

Like many users, I also looked for a method to catch the keystrokes in a DataGrid, and encountered it first in MSDN Library. So I decided to include it in the code so that users can make use of it. For most purposes, the standard KeyUp, KeyDown, and KeyPress events can capture and handle keystrokes. However, not all controls raise the standard KeyUp, KeyDown events for all keystrokes under all conditions. The DataGrid control is one of them.

If no data was assigned to the grid, the arrow keys (LEFT, RIGHT, UP, and DOWN) raise only the KeyUp event. If the DataGrid displays data, none of the standard keyboard events are raised for the navigation keys. The DataGrid is the control for which this feature is most frequently requested. You also can intercept key combinations, including CTRL and ALT. This technique does not capture the Print Screen key. In order to trap keystrokes in a Windows Forms control, you can override the ProcessCmdKey method ....
// Put this code in your Grid sub-class:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	const int WM_KEYDOWN = 0x100;
	const int WM_SYSKEYDOWN = 0x104;

	if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
	{
		switch(keyData)
		{
			case Keys.Enter:
				MessageBox.Show("Enter");
				break;

			case Keys.Down:
				MessageBox.Show("Down");
				break;

			case Keys.Up:
				MessageBox.Show("Up");
				break;

			case Keys.NumLock:
				MessageBox.Show("NumLock");
				break;

			case Keys.Escape:
				MessageBox.Show("Escape");
				break;

		} 
	}

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




>Einar:
>
> It works in normal textbox but it doesn´t work in the datagrid.column.textbox it traps any other key but not "enter" key.
>
>>Juan,
>>Does something like this work?
>>
>>private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
>>{
>>	MessageBox.Show(e.KeyChar.ToString());
>>	if (e.KeyChar == (char) System.Windows.Forms.Keys.Enter)
>>	{
>>		MessageBox.Show("Enter");
>>	}
>>	else
>>	{
>>		MessageBox.Show("Not Enter");
>>	}
>>}
>>
>>
>>I tested it on a standard textbox.
>>
>>Einar
>>
>>>Yes, it catchs KeyPress event but not "Enter" key any letter from a to z is catched.
>>>
>>>>Juan,
>>>>Does it catch the KeyPress event?
>>>>
>>>>Einar
>>>>>Thank's Einar, the problem is that the textbox of the datagrid column doesn't catch the keyup event.
>>>>>
>>>>>>>I am trying to find the way to catch when user press enter key on a datagrid cell and perform a method inside the form, i am using a selector form wich contains a datagrid as that one of Les Pinter's book in which he uses a "select" button to select the record, i could catch double click event in the textbox of the column that contains the key field through an event handler which catch's double clik on that particular textbox i want to do the same pressing "enter" key.
>>>>>>>I hope you 'd understood me and thank's in advance.
>>>>>>
>>>>>>Juan,
>>>>>>There are atleast two events you can use for this: KeyUp and KeyPress.
>>>>>>Here is some code where I used KeyUp (it is C# but it should be easy to translate into VB.NET)
>>>>>>
>>>>>>private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
>>>>>>{
>>>>>>	if (e.KeyCode == System.Windows.Forms.Keys.Enter)
>>>>>>	{
>>>>>>		MessageBox.Show("Enter button was pressed");
>>>>>>	}
>>>>>>}
>>>>>>
>>>>>>
>>>>>>Hope this helps.
>>>>>>
>>>>>>Einar
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform