This is a migrated thread and some comments may be shown as answers.

Adding items to default contect menu

12 Answers 653 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Finn
Top achievements
Rank 1
Finn asked on 28 Jul 2008, 11:37 PM
I saw an example on how to replace the default RadGridView context (right-click) menu with a "user"-defined context menu. But how do I add items to the default menu.

VB .NET code only, please.

Finn

12 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 31 Jul 2008, 07:35 AM
Hi Finn,

You can easily add items to the context menu by handling the ContextMenuOpening event of RadGridView like:
Private menuItem As RadMenuItem = Nothing 
 
        Private Sub radGridView1_ContextMenuOpening(ByVal sender As ObjectByVal e As ContextMenuOpeningEventArgs) 
            If Me.menuItem Is Nothing Then 
                Me.menuItem = New RadMenuItem("Custom Menu Item"
                AddHandler menuItem.Click, AddressOf menuItem_Click 
            End If 
 
            e.ContextMenu.Items.Add(Me.menuItem) 
        End Sub 
 
        Private Sub menuItem_Click(ByVal sender As ObjectByVal e As EventArgs) 
            MessageBox.Show("Custom Menu Item Clicked!"
        End Sub 

Don't hesitate to contact us if you have other questions.

All the best,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Finn
Top achievements
Rank 1
answered on 31 Jul 2008, 07:10 PM
Thank you Jordan.

That was easy (once one knows which even to use).

I take it that to add main menu items to the context menu I'll have to replace my existing Windows MainMenu1with the RadMenu?

Finn
0
Finn
Top achievements
Rank 1
answered on 31 Jul 2008, 10:54 PM

OK, I managed to mess it up.
I replaced my (Windows) MainMenu1with a RadMenu (a lot of work!).

Then, in the "Private Sub radGridView1_ContextMenuOpening(" procedure I added this code:
e.ContextMenu.Items(0).Visibility = Telerik.WinControls.ElementVisibility.Hidden
e.ContextMenu.Items(1).Visibility = Telerik.WinControls.ElementVisibility.Hidden
e.ContextMenu.Items.Add(RadMenuSeparatorItem1)
e.ContextMenu.Items.Add(mnuOpenMsDebugTxt)
e.ContextMenu.Items.Add(mnuExportToExcel)
e.ContextMenu.Items.Add(RadMenuSeparatorItem2)
e.ContextMenu.Items.Add(mnuRefresh)

But when doing that, these menu items disappears from the main menu!

Surely there must be a way of sharing the RadMenuItems between the main RadMenu and several content menus?

But how?

Also, as you can see, I want to get rid of the default "Copy" item.
But what I really want is to move the mnuRefresh up as the first item. How do I do that?

Lastly, how does one add a separator (without using one from another menu)?

Finn

P.S. Is there an easy way to easily convert Windows menu items to RadMenuItems? I still have several sub menus to move over from the old MainMenu1 to the RadMainMenu.

0
Jordan
Telerik team
answered on 01 Aug 2008, 10:04 AM
Hello Finn,

Glad to hear that I helped.
Now, regarding your other questions:
Yes, you should not use the windows forms context menu.
Menu items cannot be shared between several menus.
You can easily put a menu item at the beginning using the Insert method and add a separator item (see code sample below):
void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e) 
        { 
            //Insert item at the beginning of the context menu 
            e.ContextMenu.Items.Insert(0, new RadMenuItem("First Item")); 
 
            //Add a separator and another menu item at the bottom 
            e.ContextMenu.Items.Add(new RadMenuSeparatorItem()); 
            e.ContextMenu.Items.Add(new RadMenuItem("Menu Item")); 
        } 

Unfortunately you will have to manually convert your windows forms menu items to RadMenuItems.

Greetings,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Finn
Top achievements
Rank 1
answered on 01 Aug 2008, 07:41 PM
Thanks for the .Insert and Separator answers!

Also, I found the way to remove a default item:
e.ContextMenu.Items.Remove(e.ContextMenu.Items(0))

It is really disappointing that RadMenuItems can't be shared. Not very logical as they are used both in RadMenu and Context menu.

Finn
0
Jordan
Telerik team
answered on 04 Aug 2008, 04:31 PM
Hello Finn,

Glad to hear that I could help.

Regarding the sharing of RadMenuItems - they are in fact visual items and visual items cannot be shared in multiple visual element trees.
However we will consider providing some kind of menu item sharing in a future release, although I cannot promise anything at the moment.

Regards,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kevin Meyer
Top achievements
Rank 1
answered on 03 Dec 2008, 04:05 PM
It looks like with Q3 2008, e.ContextMenu is null.  I see there is a property e.ContextMenuProvider.  What's the recommended way to construct a new ContextMenu to assign to the event args?

-- Update --
It looks like the ContextMenu is only null if the grid master template is set not to allow cell context menus.
0
Jordan
Telerik team
answered on 04 Dec 2008, 07:51 PM
Hi Kevin Meyer,

Indeed, you should get the context menu if it is allowed, and there is one available in the ContextMenu property.
You can change the items in it or even set it to null. If after the event there are no items, or you have set the context menu to null, it should not show.

The ContextMenuProvider property contains a reference to the item that provided the context menu (as the name implies).

Do not hesitate to write again if you have more questions or suggestions.

Greetings,
Jordan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
MikeB
Top achievements
Rank 1
answered on 20 Feb 2009, 04:20 PM
Hi,
Is there a way to add menu items to the grid context menu without using the ContextMenuOpening Event Handler?
This is what I am currently doing (and works):

private RadMenuItem _mnuItemPrintRcpt = new RadMenuItem("Print Receipt");
private RadMenuSeparatorItem _mnuSeperator = new RadMenuSeparatorItem();



_mnuItemPrintRcpt.Click += new EventHandler(_mnuItemPrintRcpt_Click);
        private void grdCCTHistory_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            e.ContextMenu.Items.Insert(0, _mnuSeperator);
            e.ContextMenu.Items.Insert(0, _mnuItemPrintRcpt);
        }

As, I mentioned, this is working fine.  I'm just wondering if it can be done without using the ContextMenuOpening Handler?

Thanks,
MikeB





0
Jordan
Telerik team
answered on 23 Feb 2009, 01:36 PM
Hi MikeB,

You can have a context menu for the RadGridView without modifying the one from the ContextMenuOpening event arguments. You can do that by following the steps:

1. drag on your form a RadContextMenu component (this is the actual context menu)
2. drag on your form a RadContextMenuManager component (this will add a RadContextMenu property to the grid view)
3. set the RadContextMenu component to the grid RadContextMenu property
4. in the ContextMenuOpening event handler always set cancel to true.

Do not hesitate to write me back if you need further assistance.

All the best,
Jordan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
MikeB
Top achievements
Rank 1
answered on 22 Jul 2009, 04:00 PM
Hi,

I think that I didn't give you enough information on my last post.  I want to have the default grid context menu items available and add some additional items.  The response to the last post worked to replace the default context menu with my own but that is not what I want to do.  So I just went with the example that shows how to add items to the default context menu.

The reason that I am posting this now is that something seems to have changed.  We are now using version 2009_2_7001.  The first time that I right click on the grid, the context menu appears correctly (with the additional items added to the default items).  After that the context menu appears with out the added items (but has empty space where it would display).  So the added items are no longer available until the form is closed and re-opened.

What I was trying to ask in my previous post was "why would we want to add the (same) items every time that the context menu was opened?"  But it worked so I moved on.  Now it is not working and I'm wondering if this is no longer the correct method to add items to the default grid context menu.

Thanks for your help.
Mike
0
Nikolay
Telerik team
answered on 23 Jul 2009, 11:44 AM

Hi MikeB,

You can find the answer to your question in the support ticket that you opened. I am posting the answer here as well for those who are interested in the topic.

Indeed, we made changes in the context menu. In our previous versions there was a memory leak issue in the context menu which we addressed. In the current version the context menu is disposed correctly, but the items that you add to it are disposed as well. You create your menu items only once, and when you open and close the context menu, these items are disposed. So, the next time you open a context menu, you try to insert a disposed object which leads to the wrong display.

Therefore, you need to create new instances of your menu items in the ContextMenuOpening event handler:

private RadMenuItem _mnuItemPrintRcpt;     
private RadMenuSeparatorItem _mnuSeperator;     
    
void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)     
{     
    _mnuItemPrintRcpt = new RadMenuItem("Print Receipt");     
    _mnuSeperator = new RadMenuSeparatorItem();     
    e.ContextMenu.Items.Insert(0, _mnuSeperator);     
    e.ContextMenu.Items.Insert(0, _mnuItemPrintRcpt);     
}   

All the best,

Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Finn
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Finn
Top achievements
Rank 1
Kevin Meyer
Top achievements
Rank 1
MikeB
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or