开发者

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

开发者 https://www.devze.com 2023-02-10 21:21 出处:网络
Why does this never get called ? propertyGrid.KeyDown += new KeyEventHandler(propertyGrid_KeyDown); private void propertyGrid_KeyDown(object sender, KeyEventArgs e)

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 suc开发者_StackOverflowh 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.


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.


You can override this from subclass of PropertyGrid to get some key info from windows message

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)


CSharp PropertyGrid Events

// Property grid events can’t be easily subscribed to however there is way to get at the KeyUp event without impacting operation. 
// Note: The KeyDown event can be subscribed to in the same manner but the propertygrid is NOT updated with the key presses. 
// This code is added in hope it may help someone else solve the problem. It is not offered as a total solution.

// First define a class variable to indicate that events have been added.
private bool m_bPropertyGridEventsAdded = false;

public GlassInfoEntryPage(ViewBase view)
        : base(view)
    {
        InitializeComponent();

        // Subscribe to SelectedGridItemChanged
        m_PropertyGrid.SelectedGridItemChanged += M_PropertyGrid_SelectedGridItemChanged;
    }

// Now define a SelectedGridItemChanged Event Handler
private void M_PropertyGrid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
        int nXlocation;
        int nYlocation;

        PropertyGrid oPropertyGrid;
        Control oControl;

        if (m_bPropertyGridEventsAdded == false)
        {
            oPropertyGrid = (PropertyGrid)sender;

            // Search the Property Grid for a PropertyGridView Control so events can be added to it
            for (nXlocation = 0; nXlocation < oPropertyGrid.Width; nXlocation += 10)
            {
                for (nYlocation = 0; nYlocation < oPropertyGrid.Height; nYlocation += 10)
                {
                    oControl = m_glassInfoPropertyGrid.GetChildAtPoint(new Point(nXlocation, nYlocation));

                    if (oControl != null)
                    {
                        if (oControl.GetType().ToString() == "System.Windows.Forms.PropertyGridInternal.PropertyGridView")
                        {
                            // Add Events here
                            oControl.Controls[1].KeyUp += MyCode_KeyUp;
                            m_bPropertyGridEventsAdded = true;
                            break;
                        }
                    }
                }

                if (m_bPropertyGridEventsAdded == true)
                {
                    break;
                }
            }
        }
    }        

// Handle the events
private void MyCode_KeyUp(object sender, KeyEventArgs e)
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消