TrappedKeyDown doesn't work in my User Control - c#

I would like to retrieve pressed Keys in my UserControl.
The problem is that the keyboard notifications are posted to the control with the focus and this one is my windows form.
I already tried to focus the UserControl, but these approaches are not working.
For example:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) {
this.Visible = false;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
has no effect.
Source: UserControl KeyDown Event Not Fire Against Revoke Of Windows Application KeyDown Event
If I initialize my UserControl the TrappedKeyDown(KeyEventArgs e) Event is not firing.

Related

Disable Tab key on datagridview

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

Handle pressing up arrow on keyboard when focus is on button

I have to catch when the user is pressing the up arrow on the keyboard, while the button has the focus. I have written this code to handle the KeyUp event for the button:
private void btnValider_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
//do stuff
}
}
but this function didn't handle pressing the up arrow key.
I don't know if it what i want to do is possible or if i have to handle this event from the form ?
As Hans Passant suggested,
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Up && btnValider.Focused)
{
MessageBox.Show("hit");
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}
handle key down event for the button.

How to prevent ComboBox SelectionChangedEvent from firing when using keyboard arrow keys?

I have a ComboBox that's populated with a number of items.
I've specified a SelectionChangedEvent as follows:
cbo.SelectionChangedEvent += (s, e) => DoSelectionChangedThing();
This works well enough when I click an item in the ComboBox; DoSelectionChangedThing() runs as expected.
The problem is that if I use the arrow up or down keys to cycle through the items, each time I arrow up or down DoSelectionChangedThing() fires.
Is there a way to prevent this?
Capture the arrow keys on that box and disable the selection using return true.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down && comboBox1.Focused)
{
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
If you want to open the dropdown and select an item, use return false after opening.
Use dropdownclosed event instead of SelectionChangedEvent.
Occurs when the drop-down list of the combo box closes.

how to link/set the ENTER to a function in winforms without relation to a Textbox control?

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);
}

How to get Keypress event in Windows Panel control in C#

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

Categories

Resources