views:

500

answers:

4

I have a Data Grid View inside a control that is displayed in a certain area in an application.

I'd like this activity grid to refresh when F5 is pressed.

It's easy enough to do this when the Activity Grid View is the currently focused element on the screen by handling the Key Up event, but this obviously doesn't work when another element (e.g. the menu bar) was the last thing that was clicked on.

Is there a way to track key presses in this case as well? I don't have access to the code outside my data grid view/control.

The answer to this may be a clear no, but I wanted to make sure I wasn't missing something obvious in making this work.

A: 

No.

If you don't have access to the other controls that may have focus at the time, there's no way to pass the key up message from them to your control.

Jekke
A: 

You can do some global keyboard event handling on the Form the controls are on.

If you add this to your form you can get the global key events before they are send to the control.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
     case Keys.F5:
      // Send Refesh Event To Grid
      return true; // Mark Key As Handled

      // Add Any Extra Command Keys Here
    }

    return base.ProcessCmdKey(ref msg, keyData); // Resend To Base Function
}
VBNight
A: 

Have you tried to capture the event on the Form itself and then call the event handler for the data grid? You'll have to set KeyPreview to true for the form to get notified of keyboard events.

Tundey
A: 

The best way to handle this, is to get the main application form to handle all keypresses. In order to do this, set the main Form property "KeyPreview" to True.

Then, handle all your KeyUp events on the main form. More information on KeyPreview here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Jon