locked
Disable Context Menu PropertyGrid RRS feed

  • Question

  • How can I disable the context menu which appears when I click a value field inside the propertygrid?

    I was able to disable the context menu for the whole control except the value fields of the grid by setting a Context Menu I created.

    Please help me.
    Friday, October 23, 2009 11:53 AM

Answers

  • Hi void,

    The edit control of the PropertyGrid is actually a TextBox. We can get the TextBox and filter its right mouse up message to avoid the context menu being shown. This is the code snippet:

        public partial class PropertyGridDisableEditorContextMenu : Form
        {
            public PropertyGridDisableEditorContextMenu()
            {
                InitializeComponent();
            }
            private void PropertyGridDisableEditorContextMenu_Load(object sender, EventArgs e)
            {            
                propertyGrid1.SelectedObject = Color.White;
                DisableContextMenu();
            }
            private void DisableContextMenu()
            {
                //The property editor, which is actually a TextBox 
                TextBox textBox = null;
    
                //Find the edit control 
                foreach(Control propertyGridControl in (propertyGrid1 as Control).Controls)
                {
                    if (propertyGridControl.GetType().Name == "PropertyGridView")
                    {
                        foreach(Control gridViewControl in propertyGridControl.Controls)
                        {
                            if(gridViewControl.GetType().Name == "GridViewEdit")
                            {
                                textBox = gridViewControl as TextBox;
                            }
                        }
                    }
                }
    
                if(textBox != null)
                {
                    //Disable the context menu of the TextBox.
                    Application.AddMessageFilter(new RightMouseUpMessageFilter(textBox));
                }
            }
        }
        //This filter can filter the right mouse up message.
        public class RightMouseUpMessageFilter : IMessageFilter
        {
            //The control whose right mouse up message needs to be filtered.
            public Control _contrtol;
            public RightMouseUpMessageFilter(Control c)
            {
                this._contrtol = c;
            }
            public bool PreFilterMessage(ref Message m)
            {
                if (!this._contrtol.IsDisposed && m.HWnd == this._contrtol.Handle)
                {
                    //WM_RBUTTONUP mouse right button up
                    int rightMouseUpMessage = 0x0205;
                    if (m.Msg == rightMouseUpMessage)
                    {
                        //Filter the message.
                        return true;
                    }
                }
                return false;
            }
        }



    You can get more about Application.AddMessageFilter method from:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx

    Let me know if this does not help.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by void Func Friday, October 30, 2009 3:10 AM
    Monday, October 26, 2009 9:46 AM

All replies

  • Hi void,

    The edit control of the PropertyGrid is actually a TextBox. We can get the TextBox and filter its right mouse up message to avoid the context menu being shown. This is the code snippet:

        public partial class PropertyGridDisableEditorContextMenu : Form
        {
            public PropertyGridDisableEditorContextMenu()
            {
                InitializeComponent();
            }
            private void PropertyGridDisableEditorContextMenu_Load(object sender, EventArgs e)
            {            
                propertyGrid1.SelectedObject = Color.White;
                DisableContextMenu();
            }
            private void DisableContextMenu()
            {
                //The property editor, which is actually a TextBox 
                TextBox textBox = null;
    
                //Find the edit control 
                foreach(Control propertyGridControl in (propertyGrid1 as Control).Controls)
                {
                    if (propertyGridControl.GetType().Name == "PropertyGridView")
                    {
                        foreach(Control gridViewControl in propertyGridControl.Controls)
                        {
                            if(gridViewControl.GetType().Name == "GridViewEdit")
                            {
                                textBox = gridViewControl as TextBox;
                            }
                        }
                    }
                }
    
                if(textBox != null)
                {
                    //Disable the context menu of the TextBox.
                    Application.AddMessageFilter(new RightMouseUpMessageFilter(textBox));
                }
            }
        }
        //This filter can filter the right mouse up message.
        public class RightMouseUpMessageFilter : IMessageFilter
        {
            //The control whose right mouse up message needs to be filtered.
            public Control _contrtol;
            public RightMouseUpMessageFilter(Control c)
            {
                this._contrtol = c;
            }
            public bool PreFilterMessage(ref Message m)
            {
                if (!this._contrtol.IsDisposed && m.HWnd == this._contrtol.Handle)
                {
                    //WM_RBUTTONUP mouse right button up
                    int rightMouseUpMessage = 0x0205;
                    if (m.Msg == rightMouseUpMessage)
                    {
                        //Filter the message.
                        return true;
                    }
                }
                return false;
            }
        }



    You can get more about Application.AddMessageFilter method from:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx

    Let me know if this does not help.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by void Func Friday, October 30, 2009 3:10 AM
    Monday, October 26, 2009 9:46 AM
  • Hi Aland Li,

    The solution you specified worked. Thank you very much.

    Manu
    Friday, October 30, 2009 3:03 AM