events in parent and child

  • Thread starter Thread starter vidalsasoon
  • Start date Start date
V

vidalsasoon

I have a form that declares new form and both are shown. When my parent
form has focus it handles events correctly using this function:

protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{

case Keys.Escape:
this.Close();
break;
case Keys.P:
MessageBox.Show("debug");
p.LastName = "test";
break;
//default:
// break;
}
}

when I use this function from my child form, nothing happens (i thought
it would automatically be called just as the parent form did).
What am i missing?
 
I looked a little deeper into my problem and saw that as soon as I add
any control to my form, my "protected override void OnKeyDown" method
stops getting picked up. any reason why this happens?
 
The controls you have added are getting the keystrokes before the form does,
and if they handle the keystroke, then the form will not see it.

You can try setting the form's KeyPreview property to "true". Or, if it's
command keys you want to handle, you may be better off overriding
ProcessCmdKey, something like this:
protected override bool ProcessCmdKey(ref Message msg,Keys keyData)
{
// If the "current gridview" property is assigned, then all
// Up/Down arrows should navigate the grid
if ( gvCurrent == null )
{
return base.ProcessCmdKey(ref msg, keyData);
}
if ( gvCurrent.GridControl.Enabled == false )
{
return base.ProcessCmdKey(ref msg, keyData);
}
switch (keyData)
{
case Keys.Up:
gvCurrent.MovePrev();
return true;
case Keys.Down:
gvCurrent.MoveNext();
return true;
case Keys.PageUp:
gvCurrent.MovePrevPage();
return true;
case Keys.PageDown:
gvCurrent.MoveNextPage();
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}
 
Back
Top