Thursday, May 11, 2006

C# Beautified Menu Entries


There are not many examples of how to do things in C# in a beautiful way. Even the O'Reilly examples are sometimes pretty crufty. Here's an example of how to create an entire menu, including submenus without any goofy placeholder variables.



public void CreateMenus() {
MenuStrip menu = new MenuStrip();
menu.Items.AddRange( new ToolStripItem[]{
new ToolStripMenuItem("&File", null,
new ToolStripMenuItem("&Close", null, new EventHandler(CloseMenu_Click)),
new ToolStripMenuItem("E&xit", null, new EventHandler(ExitMenu_Click)))
,
new ToolStripMenuItem("&Edit", null,
new ToolStripMenuItem("&New Thing", null, new EventHandler(EditNewThingMenu_Click)))
,
new ToolStripMenuItem("&Pen", null,
new ToolStripMenuItem("&Pencil", null, new EventHandler(PenMenu_Click)),
new ToolStripMenuItem("Bl&ack Pen", null, new EventHandler(PenMenu_Click)),
new ToolStripMenuItem("Bl&ue Pen", null, new EventHandler(PenMenu_Click)),
new ToolStripMenuItem("Eraser", null, new EventHandler(PenMenu_Click)))
});
foreach( ToolStripItem i in menu.Items)
i.Font = Global.MenuFont;
Global.ExplorerForm.MainMenuStrip = menu;
}

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?