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

3 posts, 1 answers
  1. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 19 Sep Link to this post

    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. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 21 Sep Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 24 Sep in reply to Dess Link to this post

    perfect! Thanks.

    Erwin

Back to Top