Dynamicly Adding to ContextMenu

13 posts, 1 answers
  1. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 15 Dec 2015 Link to this post

    I am unable to figure out how to add to the ContextMenu of a RadMultiColmnComboBox or a RadDropDownList. If I instantiate their ContextMenu property, it is only displayed when clicking the very edge of the control, which tells me there is a child or editor control laying ontop. But when I try to modify the EditorControl's ContextMenu through the ContextMenuOpening event, the event never fires.

    radMultiColumnComboBox.EditorControl.ContextMenuOpening += (sender, args) =>
    {
        Console.WriteLine("ContextMenuOpening event");
    };
     This never fires.

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 16 Dec 2015 Link to this post

    Hello Paul,

    Thank you for writing.

    Firstly, I would like to note that when you click over the grid popup, the drop down is closed. In order to show the context menu in the popup grid, it is necessary to set the RadMultiColumnComboBox.EditorControl.AllowCellContextMenu and AllowColumnHeaderContextMenu properties to true. Then, the ContextMenuOpening event will be fired and you can assign the desired menu:
    this.radMultiColumnComboBox1.EditorControl.ContextMenuOpening+=EditorControl_ContextMenuOpening;

    private void EditorControl_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
    {
        e.ContextMenu = new RadDropDownMenu();
        e.ContextMenu.Items.Add(new RadMenuItem("test item"));
    }

    public Form1()
    {
        InitializeComponent();
        this.radMultiColumnComboBox1.EditorControl.AllowCellContextMenu = true;
        this.radMultiColumnComboBox1.EditorControl.AllowColumnHeaderContextMenu = true;
    }

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 16 Dec 2015 in reply to Dess Link to this post

    Unfortunately, that did not resolve the issue. That allowed me to display a ContextMenu for the drop-down GridView, but it does not allow me to edit the ContextMenu when right-clicking the RadMultiColmnComboBox itself (when the drop-down is NOT shown).
  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 17 Dec 2015 Link to this post

    Hello Paul,

    Thank you for writing back.

    When you right click the editable part of RadMultiColumnComboBox, the default context menu for the hosted MS TextBox is shown. You can remove its context menu by assigning a new ContextMenu instance to its ContextMenu property:
    this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl.ContextMenu = new ContextMenu();

    Thus, you can add your custom items to the ContextMenu.
    ContextMenu cm = new ContextMenu();
    cm.MenuItems.Add(new MenuItem("item1"));
    this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl.ContextMenu = cm;

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  6. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 22 Dec 2015 in reply to Dess Link to this post

    Sorry for the delay in response.

    That did allow me to add my own menu which resolves my issue. However, doing so removes all the default ContextMenu items. For knowledge sake, is there no way to just insert into the default ContextMenu?

  7. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 23 Dec 2015 Link to this post

    Hello,

    I am not sure if it is possible, but even if it is it will require working with Windows API to do it and then we have to see it we can adopt the approach to the textbox RadMultiColumnComboBox shows. Much easier approach is the one you currently have.

    You could even use RadContextMenu, so your menus will be properly themed:
    this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl.ContextMenu = new ContextMenu();
     
       RadContextMenu menu = new RadContextMenu();
       menu.Items.Add(new RadMenuItem("test item"));
     
       RadContextMenuManager mgr = new RadContextMenuManager();
       mgr.SetRadContextMenu(radMultiColumnComboBox1, menu);
       mgr.SetRadContextMenu( this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl, menu);

    I hope that you find this information useful.

    Regards,
    Stefan
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  8. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 22 Aug in reply to Stefan Link to this post

    This works, but also displays the grid. Is there a way to prevent the grid from displaying when the click event is a right-click?
  9. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 23 Aug Link to this post

    Hello Paul,

    Thank you for writing. 

    The popup grid will be displayed with right mouse down only if you click the arrow button, but not the editable part. You can disable opening the drop down in this case by canceling the DropDownOpening event. Here is a sample code snippet demonstrating how to achieve it: 
    public Form1()
    {
        InitializeComponent();
     
        this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl.ContextMenu = new ContextMenu();
     
        RadContextMenu menu = new RadContextMenu();
        menu.Items.Add(new RadMenuItem("test item"));
     
        RadContextMenuManager mgr = new RadContextMenuManager();
        mgr.SetRadContextMenu(radMultiColumnComboBox1, menu);
        mgr.SetRadContextMenu(this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.TextBoxControl, menu);
     
        this.radMultiColumnComboBox1.MultiColumnComboBoxElement.ArrowButton.MouseDown += ArrowButton_MouseDown;
        this.radMultiColumnComboBox1.DropDownOpening += radMultiColumnComboBox1_DropDownOpening;
    }
     
    private void radMultiColumnComboBox1_DropDownOpening(object sender, CancelEventArgs args)
    {
        if (this.radMultiColumnComboBox1.Tag == "cancel")
        {
            args.Cancel = true;
            this.radMultiColumnComboBox1.Tag = null;
        }
    }
     
    private void ArrowButton_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            this.radMultiColumnComboBox1.Tag = "cancel";
        }
    }

    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.
  10. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 23 Aug Link to this post

    Its displaying the grid when I right-click on the editable part of the control. I will try your solution and see if it helps.
  11. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 24 Aug Link to this post

    Hello Paul, 

    Thank you for writing back. 

    I was unable to reproduce the issue you are facing. I have attached my sample project for your reference. If you are experiencing any further difficulties, feel free to modify the project in a way to reproduce the experienced issue and get back to me with it so I can investigate the precise case. Thank you in advance. 

    I am looking forward to your reply.

    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.
  12. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 24 Aug Link to this post

    I found it. 

    this.radMultiColumnComboBox1.DropDownStyle = RadDropDownStyle.DropDownList;

    Fixed it with this:

    this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.TextBoxItem.Click += (sender, args) =>
    {
        var x = args as MouseEventArgs;
        if (x.Button == MouseButtons.Right)
        {
            if (ddl.DataBindings.Count == 0) return;
     
            var o = ddl.SelectedValue as IBoundObject;
     
            if (o == null) return;
     
            var items = controller.GetContextMenuItems(o, null);
     
            if (items.Length == 0) return;
     
            RadContextMenu menu = new RadContextMenu();
            menu.Items.AddRange(items);
            menu.Show(ddl, x.X, x.Y);
     
            this.radMultiColumnComboBox1.MultiColumnComboBoxElement.EditorControl.Parent.Hide();
        }
    };

    Is there a better way of accomplishing this?

  13. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 26 Aug Link to this post

    Hello Paul, 

    Thank you for writing back. 

    Indeed, when the DropDownStyle property is set to DropDownList, the popup is opened by right mouse down. Cancel the DropDownOpening event when you click the editable part. You can handle this case by subscribing to the MultiColumnComboBoxElement.TextBoxElement.MouseDown event and use the Tag property for example:
    this.radMultiColumnComboBox1.MultiColumnComboBoxElement.TextBoxElement.MouseDown += TextBoxElement_MouseDown;
    private void TextBoxElement_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            this.radMultiColumnComboBox1.Tag = "cancel";
        }
    }
     
    private void radMultiColumnComboBox1_DropDownOpening(object sender, CancelEventArgs args)
    {
        if (this.radMultiColumnComboBox1.Tag == "cancel")
        {
            args.Cancel = true;
            this.radMultiColumnComboBox1.Tag = null;
        }
    }

    I hope this information helps. If you have any additional questions, please let me know.

    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.
  14. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 26 Aug in reply to Dess Link to this post

    WONDAVA!! Thaqnk you! Works great.
Back to Top
UI for WinForms is Visual Studio 2017 Ready