Hide RadContextMenu

7 posts, 2 answers
  1. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 07 Apr 2011 Link to this post

    Hi,

    I am using a RadContextMenu and a spell checker on a richtextbox. When I right click on text, the RadContextMenu and the spell check menu are showing up. I want to hide the RadContextMenu when a user right clicks.

    Following is the code I am using to accomplish that, but I think this is not the right way as a horizontal line shows up when I try to collapse the menu items part of the RadContextMenu. Also for the first time the RadContextMenu shows up even before I get into the mouseup event.

    Please suggest me a way to hide the RadContextMenu.


       private void rtbComments_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (rtbComments.SelectionLength > 0)
                    {
                        mnuCut.Visibility = ElementVisibility.Visible;
                        mnuCopy.Visibility = ElementVisibility.Visible;
                        mnuPaste.Visibility = ElementVisibility.Visible;
                    }
                    else
                    {
                        // Check if the words under the cursor are misspelled.
                        int x = SpellingCheckerEngine.CheckCtrlBackgroundMenu(rtbComments.Handle, e.X, e.Y, RTB1Options, RTB1MarkColor);
                        // If there were no misspelled words under the cursor, popup the copy menu.
                        if (x == 0)
                        {
                            mnuCut.Visibility = ElementVisibility.Visible;
                            mnuCopy.Visibility = ElementVisibility.Visible;
                            mnuPaste.Visibility = ElementVisibility.Visible;
                        }
                        else
                        {
                            mnuCut.Visibility = ElementVisibility.Collapsed;
                            mnuCopy.Visibility = ElementVisibility.Collapsed;
                            mnuPaste.Visibility = ElementVisibility.Collapsed;
                        }
                    }
                }
            }
  2. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 07 Apr 2011 Link to this post

    Hi,

    I think I got a little bit close by doing the following. Still need to find out a way to hide the RadContextMenu before the control gets into the mouseup event.

    private void rtbComments_MouseUp(object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
             {
                 if (rtbComments.SelectionLength > 0)
                 {
                     radContextMenuManager1.SetRadContextMenu(rtbComments, cMenuComments);
                 }
                 else
                 {
                     // Check if the words under the cursor are misspelled.
                     int x = SpellingCheckerEngine.CheckCtrlBackgroundMenu(rtbComments.Handle, e.X, e.Y, RTB1Options, RTB1MarkColor);
                     // If there were no misspelled words under the cursor, popup the copy menu.
                     if (x == 0)
                     {
                         radContextMenuManager1.SetRadContextMenu(rtbComments, cMenuComments);
                     }
                     else
                     {
                         radContextMenuManager1.SetRadContextMenu(rtbComments, null);
                     }
                 }
             }
         }
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 12 Apr 2011 Link to this post

    Hello Jack,

    Thank you for writing.

    I would suggest the following approach:

    void cMenuComments_DropDownOpening(object sender, CancelEventArgs e)
    {
        Point location = this.rtbComments.PointToClient(Control.MousePosition);
        if (SpellCheck(location))
        {
            e.Cancel = true;
        }
    }

    The idea behind it is to do your logic in the DropDownOpening event and cancel it if needed. This way you won't have to set and unset context menus to the context menu manager and the popup won't show neither.

    I hope you find this useful. If you need further help, do not hesitate to write back.

    Best wishes,
    Ivan Todorov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 12 Apr 2011 Link to this post

    Hi Ivan,

    That did work. Thanks a lot. I have one other question related to RadContextMenu in a RadGridView 

    I am using a ColumnChooser and a RadContextMenu on a RadGridView . How do I hide the RadContextMenu  when the user clicks on the column header. 

    Following is the code I am currently using which does work but I would like to use the DropDownOpening event of cMenuReports to hide it since that seems to be a cleaner way of doing things as you suggested earlier.


    So, the bottom line is how do I know if the user right clicked on a column or a cell in the DropDownOpening event of cMenuReports.

    The attached image should give you an idea of the problem.(RadContextMenu on top of ColumnChooser)

    private void rgvReports_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
            if (e.ContextMenuProvider is GridHeaderCellElement)
            {
                radContextMenuManager1.SetRadContextMenu(rgvReminders, null);
            }
            else
            {
                radContextMenuManager1.SetRadContextMenu(rgvReminders, cMenuReports);
            }
    }
  6. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 15 Apr 2011 Link to this post

    Hello Jack,

    I am glad I could help.

    As to your question, here is the approach I would suggest:

    void radContextMenu1_DropDownOpening(object sender, CancelEventArgs e)
    {
        Point mouseLocation = radGridView1.PointToClient(Control.MousePosition);
        RadElement elementUnderMouse = radGridView1.ElementTree.GetElementAtPoint(mouseLocation);
     
        if (elementUnderMouse is GridHeaderCellElement)
        {
            e.Cancel = true;
        }
    }

    In general, this code shows how you can get the element of the grid which is under the mouse pointer. When you have this element you can check its type and decide whether you want to cancel the dropdown. You can check the type of a cell by spying it with our RadControlSpy tool. More information about using it can be found here.

    I hope this will help you. Do not hesitate to contact me if you have any additional questions.

    Best wishes,
    Ivan Todorov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 15 Apr 2011 Link to this post

    Thanks Ivan,

    It worked perfectly.
  8. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 20 Apr 2011 Link to this post

    Hello Jack,

    I am glad I could help. If you have any additional questions, feel free to contact me.

    Greetings,
    Ivan Todorov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready