Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the PropertyGrid raise Keyboard/Mouse events?

Tags:

c#

winforms

Why does this never get called ?

        propertyGrid.KeyDown += new KeyEventHandler(propertyGrid_KeyDown);

        private void propertyGrid_KeyDown(object sender, KeyEventArgs e)
        {
           PoorLittleMethod(); //Never gets called
        }

This seems to be the same for Mouse event

I'veread on some forums that PGrid is tricky on raising such events as it Inherits them from Control but does not really Raise them. is that true ? If yes, how to bypass that ?

EDIT 1:
As this seems to be "regular", I find it very light from MS not to specify this explicitely on the MSDN Reference of the propertyGrid class and leave events "as is" as if they were usable, whereas they are not. Tricky things like these are at least usually specified in "notes" inside the refs.

EDIT 2:
I am presently coding a workaround. I'll be posting it soon.

like image 211
Mehdi LAMRANI Avatar asked Feb 14 '11 11:02

Mehdi LAMRANI


1 Answers

The PropertyGrid's KeyDown property is marked as Browsable(false) - presumably the conclusion we can take from this is that it is not supported in an of itself but is in fact present as a side-effect of its inheritance hierarchy.

Though, interestingly enough, its EditorBrowsable attribute (which is also a designer indicator, for Intellisense and the suchlike) is set as EditorBrowsableState.Advanced - where we would expect EditorBrowsableState.Never should the former presumption be true.

Some information from MSDN forums outlines the why of this situation:

From the tool UI Spy we can see the PropertyGrid is a just a panel and it consists of three Windows Controls. Our KeyDown event should be processed by the child control table. The structure:

-"pane" "PropertyGrid"
  --"pane" "Description Pane"
  --"table" "Properties Window"
  --"tool bar" "ToolBar"

The suggested solution (also provided in the MSDN link) to overcoming this is to use native system calls to retrieve window/control information, subclass NativeWindow and override the WndProc method to handle the events you like, KeyDown in this case.

like image 144
Grant Thomas Avatar answered Oct 19 '22 22:10

Grant Thomas