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

Dynamicly Adding to ContextMenu

12 Answers 349 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 15 Dec 2015, 08:55 PM

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.

12 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Dec 2015, 12:59 PM
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
0
Paul
Top achievements
Rank 1
answered on 16 Dec 2015, 03:15 PM
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).
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Dec 2015, 10:22 AM
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
0
Paul
Top achievements
Rank 1
answered on 22 Dec 2015, 03:22 PM

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?

0
Stefan
Telerik team
answered on 23 Dec 2015, 08:58 AM
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. 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
0
Paul
Top achievements
Rank 1
answered on 22 Aug 2016, 04:27 PM
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?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Aug 2016, 06:45 AM
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.
0
Paul
Top achievements
Rank 1
answered on 23 Aug 2016, 12:56 PM
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.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Aug 2016, 12:57 PM
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.
0
Paul
Top achievements
Rank 1
answered on 24 Aug 2016, 01:19 PM

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?

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 26 Aug 2016, 06:16 AM
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.
0
Paul
Top achievements
Rank 1
answered on 26 Aug 2016, 12:35 PM
WONDAVA!! Thaqnk you! Works great.
Tags
ContextMenu
Asked by
Paul
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Paul
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or