Trapping up/down arrow keys

G

Guest

I have a class that derives from a Windows forms datagrid and of instance of
this class is hosted within a Windows forms user control. In the derived
class, I have overridden the ProcessDialogKey method and the
PreProcessMessage methods in hopes of trapping a press of the up and down
arrow keys. However, these methods never execute. It seems something else
is intercepting those key strokes, but I am at a loss.

What am I missing?

Thanks for you help.

Chris

Here is the code for the overridden methods:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
MessageBox.Show("ProcessDialogKey: " + keyData.ToString());
}

return base.ProcessDialogKey (keyData);
}

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN && (keyCode == Keys.Down || keyCode == Keys.Up) )
{
MessageBox.Show("PreProcessMessage: " + keyCode.ToString());
}
return base.PreProcessMessage(ref msg);
}
 
V

Vlado

Chris Kormann said:
I have a class that derives from a Windows forms datagrid and of instance
of
this class is hosted within a Windows forms user control. In the derived
class, I have overridden the ProcessDialogKey method and the
PreProcessMessage methods in hopes of trapping a press of the up and down
arrow keys. However, these methods never execute. It seems something
else
is intercepting those key strokes, but I am at a loss.

What am I missing?

Thanks for you help.

Chris

Here is the code for the overridden methods:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
MessageBox.Show("ProcessDialogKey: " + keyData.ToString());
}

return base.ProcessDialogKey (keyData);
}

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN && (keyCode == Keys.Down || keyCode ==
Keys.Up) )
{
MessageBox.Show("PreProcessMessage: " + keyCode.ToString());
}
return base.PreProcessMessage(ref msg);
}


First of all I think it's not good to call MessageBox.Show from there.
Instead you should put there e.g.

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
this.SomeMethod();
return true;
}
return base.ProcessDialogKey (keyData);
}

Also, you can try with overriding ProcessCmdKey

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top