Joke Collection Website - Joke collection - C# winform dynamically generates menus with contextmenustrip

C# winform dynamically generates menus with contextmenustrip

Sample code:

Protected coverage void OnLoad(EventArgs e)

{

Base. Load (e);

//Add menu 1

ToolStripMenuItem subitem;

SubItem = AddContextMenu ("news ",context menustrip 1.items,null);

//Add a submenu

AddContextMenu ("Today's news", sub-item. Drop-down item, new event handler (menu clicked));

AddContextMenu ("today's concern", sub-item. DropdownItems, New Eventhandler);

//Add Menu 2

SubItem = AddContextMenu ("joke ",context menustrip 1.items,null);

//Add a submenu

AddContextMenu ("modern joke", subtitle. Drop-down item, new event handler (menu clicked));

AddContextMenu ("ancient joke", sub-item. Drop-down item, new event handler (menu clicked));

}

///& lt; Summary & gt

///Add a submenu

///& lt; /summary & gt;

///& lt; Param name = "text"> The text to be displayed. If it is-,it will be displayed as a separator.

///& lt; Param name = "cms"> submenu set to add to.

///& lt; Param name = "callback"> Event triggered when clicked.

///& lt; The submenu generated by returns & gt returned null.

Toolstrip menuitem add context menu (string text, ToolStripItemCollection cms, EventHandler callback)

{

if (text == "-")

{

toolstrip separator tsp = new toolstrip separator();

cms。 Add (tsp);

Returns null

}

else if(! String. IsNullOrEmpty(text))

{

toolstrip menuitem tsmi = new toolstrip menuitem(text);

If (callback! = null) tsmi。 Click+= Call Back;

cms。 add(tsmi);

Return to tsmi

}

Returns null

}

Void MenuClicked (object sender, event parameter e)

{

MessageBox。 Show ("You clicked the menu item ["+((sender as ToolStripMenuItem)). text)+"]";

}