I have a custom control MyLookUpEdit, it inherites Devexpress LookUpEdit.
In MyLookUpEdit, I have below method:
protected override bool ProcessDialogKey(Keys keyData)
I use this custom control (let's call it nameLookUpEdit) on a form. When I tab through the control, I see ProcessDialogKey is called and it focus to next control according to tabIndex.
Now we hope when enter is pressed, our control can behavior exactly the same as tab pressed. I think I should add in MyLookUpEdit one of those methods:
protected override void OnKeyDown(KeyEventArgs e)
or
protected override void OnEditorKeyDown(KeyEventArgs e)
and capture the enter key and hence call the ProcessDialogKey explicitly:
{
if(e.KeyData == Keys.Enter)
{
ProcessDialogKey(Keys.Tab);
}
base....
}
But when I test on it, I find when nameLookUpEdit is currently focused, I press enter key, neither onKeyDown nor onEditorKeyDown will be called.
Why this happens?
And what is the correctly way to let enter behavior the same as tab?
try overriding ProcesscmdKey ?
edit: Added the return statement for correctness.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
//do stuff
}
return base.ProcessCmdKey(ref msg, keyData);
}
Related
Here is the code:
browserControl.KeyDown += new System.Windows.Forms.KeyEventHandler(BrowserControl_KeyDown);
private void BrowserControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
MessageBox.Show("E");
}
I assumed that when i entered the browserControl (a CefSharp ChromiumWebBrowser), it will simply give me a message box with the message "E". Obviously! But no, absolutely nothing i am trying to make something there but i don't think i would download a browser where you get stuck in fullscreen mode because none of the shortcut keys work.
Here is another solution i searched up and appearently should work, and it does if i click a button outside the browser, but not when i need it:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.E)
{
MessageBox.Show("E.. A... GAMES");
}
return base.ProcessCmdKey(ref msg, keyData);
}
I mean come on! I am overriding the whole key process, and still gives me absolutely nothing. What?
I am trying to disable tab key from datagridview, also to create my own event on it. Also if it is possible to disable up,down,right,left and enter key.
OnLoad Event
this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
On KeyDownEvent
private void gridInvoice_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
//SelectNextControl(dataGridView1, true, true, true, true);
// or Parent.SelectNextControl() if the grid is an only child, etc.
e.Handled = true;
}
}
With code above tab key it works. I moves to next cell. How can i prevent this?
You have to use PreviewKeyDown event instead of KeyDown.
According to Microsoft Control.PreviewKeyDown Event Description
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses.
You need to insert the code below in PreviewKeyDown event if you want to use KeyDown event when Tab is pressed.
if (e.KeyCode == Keys.Tab) { e.IsInputKey = true; }
When in editmode thanks to Jimi
I'm sorry I thought the problem too simple. How about this. You can override ProcessCmdKey to ignore Tab when you in editmode of your DGV. Is it too brute?
I think this is simple than make a new edit control but not elegance too.
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Tab && dataGridView1.EditingControl != null) { return true; }
else return base.ProcessCmdKey(ref msg, keyData);
}
from Similar problem
I currently have the following event handler which catches the Ctrl+Enter key combo in my Form code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && e.Control)
{
// Stuff
}
}
I also have two non-ReadOnly TextBoxes in the form, one of which is multiline, while the other one isn't. Whenever I hit Ctrl+Enter, the event does get handled, but it also registers as an Enter keypress when the focus is in either TextBox. What I want to do is register the key combo without the Enter keypress modifying the text in either box. Is there any way I could go about doing this?
Your best choice is use ProcessCmdKey: Just add this to your Form add it will work:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Enter))
{
// Stuff
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
You should use the PreviewKeyDown event instead and set the IsInputKey property accordingly:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter && e.Control)
{
// Stuff
e.IsInputKey = false;
}
}
UPDATE: From the name of your handler, I guess you added it to the Form's KeyPress/KeyDown/PreviewKeyDown event. Instead you should register the method I showed above with each TextBox's PreviewKeyDown event.
To not destroy what already works for you, you may leave your code as it is and just add a handler to the TextBox's PreviewKeyDown event where you set IsInputKey to false for the specified keys, but don't do your // Stuff.
I'm using Winforms.
I've a screen approx. 10 fields. and a Update button.
But I don't want to use neither show on screen a button (btnUpdate).
I just want to show the the fields, they can change some values and by pressing the enter it should execute a function in code behind.
I googled and find some solutions like KeyPress on TextBox or whatever, but I don't want to link this to a TextBox. Then I found form.Acceptbutton = btnUpdate... but then I have to use a button on my designer.
so how can I make a situtation by not USING a Button control to do an update (in other words executing function in code-behind by pressing the Enter Key).
Try overriding the ProcessCmdKey
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Return)
{
//Raise Update Event
return true;
}
else if (keyData == Keys.Escape)
{
//Raise Cancel Event
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
i want to get keypress event in windows panel control in c#, is any body help for me...
You should handle the Panel.KeyPress event.
Example
public void MyKeyPressEventHandler(Object sender, KeyPressEventArgs e)
{
... do something when key is pressed.
}
...
(MyPanel as Control).KeyPress += new KeyPressEventHandler(MyKeyPressEventHandler);
The problem is, that at first your main form got the KeyPress and will immediately send this message to the active control. If that doesn't handle this key press it will be bubbled up to the parent control and so on.
To intercept this chain, you have to in your Form.KeyPreview to true and add an handler to Form.KeyPress. Now you can handle the pressed key within your form.
"Panel" objects cannot receive the "KeyPress" event correctly.
I've created Panel overload:
public class PersoPanel : Panel
and used the overridden method ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
to intercept pressed keys:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
MessageBox.Show("You press " + keyData.ToString());
// dO operations here...
return base.ProcessCmdKey(ref msg, keyData);
}
Panel + Keypress - C# Discussion Boards - CodeProject
http://www.codeproject.com/Messages/704386/Panel-plus-Keypress.aspx