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

Using RadContextMenu instead in of standard context menu in a derived control

2 Answers 188 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
erwin
Top achievements
Rank 1
Veteran
Iron
erwin asked on 19 Sep 2016, 02:28 PM

I have a control that is derived from RadLabel. Inside this control I would like to add a RadContext menu instead of the standard Windows Forms context menu. What's the proper way to do this inside my control? I don't want to rely on a ContextMenuManager on the parent form but instead encapsulate everything in my control.

Regards

Erwin

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Sep 2016, 12:01 PM
Hello Erwin,

Thank you for writing.  

The suitable approach for assigning a RadContextMenu to any Control is to use a RadContextMenuManager as it is demonstrated in the following help article  http://docs.telerik.com/devtools/winforms/menus/contextmenu/assign-radcontextmenu-to-telerik-and-non-telerik-controls 

However, if you don't want to use it, it is possible to handle the MouseDown event of the control and show programmatically the context menu. Here is a sample code snippet for RadLabel:
public class CustomLabel : RadLabel
{
    RadContextMenu menu;
 
    public override string ThemeClassName 
    {
        get
        {
            return typeof(RadLabel).FullName; 
        }
    }
 
    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            if (menu == null)
            {
                menu = new RadContextMenu();
                menu.Items.Add(new RadMenuItem("Item1"));
                menu.Items.Add(new RadMenuItem("Item2"));
            }
            menu.Show(this, e.Location);
            return;
        }
        base.OnMouseDown(e);
    }
}

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 24 Sep 2016, 08:54 PM

perfect! Thanks.

Erwin

Tags
ContextMenu
Asked by
erwin
Top achievements
Rank 1
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
erwin
Top achievements
Rank 1
Veteran
Iron
Share this question
or