This is a migrated thread and some comments may be shown as answers.
Hide RadContextMenu
6 Answers 214 Views
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 07 Apr 2011, 05:00 PM
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;
                    }
                }
            }
        }

6 Answers, 1 is accepted

Sort by
0
Jack
Top achievements
Rank 1
answered on 07 Apr 2011, 05:20 PM
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);
                 }
             }
         }
     }
0
Accepted
Ivan Todorov
Telerik team
answered on 12 Apr 2011, 02:27 PM
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
0
Jack
Top achievements
Rank 1
answered on 12 Apr 2011, 07:45 PM
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);
        }
}
0
Accepted
Ivan Todorov
Telerik team
answered on 15 Apr 2011, 09:13 AM
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
0
Jack
Top achievements
Rank 1
answered on 15 Apr 2011, 02:49 PM
Thanks Ivan,

It worked perfectly.
0
Ivan Todorov
Telerik team
answered on 20 Apr 2011, 11:48 AM
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
Asked by
Jack
Top achievements
Rank 1
Answers by
Jack
Top achievements
Rank 1
Ivan Todorov
Telerik team
Share this question
or