Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add items to a Context Menu & set Click action

I have a List of strings that is regenerated every 5 seconds. I want to create a Context Menu and set its items dynamically using this list. The problem is that I don't have even a clue how to do that and manage the Click action for every item generated (which should use the same method with different parameter DoSomething("item_name")).

How should I do this?

Thanks for your time. Best regards.

like image 205
Tute Avatar asked Oct 22 '08 11:10

Tute


People also ask

Can dynamically change a menu by?

Change menu items dynamically in JavaScript (ES5) ContextMenu control. The items visible in the ContextMenu can be changed dynamically based on the target in which you open the ContextMenu.

How can you attach context menu to a control?

In the Link to Command designer, change the following command properties. Select ContextMenu from the Create a new command listbox. This will create a command with a submenu which can be used as a context menu. Select OK, in the Link to Command dialog box.

What are context menu items?

A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.

What are context menu commands?

A context menu offers a list of relevant commands that apply to the current selection or task. The pen user can select commands near the selected object rather than move his or her hand across the screen to the menu or toolbar.


2 Answers

Another alternative using a ToolStripMenuItem object:

//////////// Create a new "ToolStripMenuItem" object:
ToolStripMenuItem newMenuItem= new ToolStripMenuItem();

//////////// Set a name, for identification purposes:
newMenuItem.Name = "nameOfMenuItem";

//////////// Sets the text that will appear in the new context menu option:
newMenuItem.Text = "This is another option!";

//////////// Add this new item to your context menu:
myContextMenuStrip.Items.Add(newMenuItem);


Inside the ItemClicked event of your myContextMenuStrip, you can check which option has been chosen (based on the name property of the menu item)

private void myContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    ToolStripItem item = e.ClickedItem;

    //////////// This will show "nameOfMenuItem":
    MessageBox.Show(item.Name, "And the clicked option is...");
}
like image 115
tomloprod Avatar answered Nov 14 '22 09:11

tomloprod


So, you can clear the items from the context menu with:

myContextMenuStrip.Items.Clear();

You can add an item by calling:

myContextMenuStrip.Items.Add(myString);

The context menu has an ItemClicked event. Your handler could look like so:

private void myContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    DoSomething(e.ClickedItem.Text);
}

Seems to work OK for me. Let me know if I misunderstood your question.

like image 37
itsmatt Avatar answered Nov 14 '22 08:11

itsmatt