I would like to re-write this C# code so it is easier to understand. Should I create a separate function to configure a menu item? Is there is some kind of using
statement so I only need to mention the menu name once?
private void initializeContextMenuRightClick()
{
try
{
ctxMenuStripRightClick = new ContextMenuStrip();
ctxMenuStripRightClick.Opening += ctxMenuStrip_Opening;
ToolStripMenuItem addMenuItem = new ToolStripMenuItem("Add", null, AddMenuItem_Click);
ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem("Delete", null, DeleteMenuItem_Click);
ToolStripMenuItem editMenuItem = new ToolStripMenuItem("Edit", null, EditMenuItem_Click);
ToolStripMenuItem markDoneMenuItem = new ToolStripMenuItem("Mark Done", null, MarkDoneMenuItem_Click);
ToolStripSeparator separatorMenuItem = new ToolStripSeparator();
ctxMenuStripRightClick.Items.Add(addMenuItem);
ctxMenuStripRightClick.Items.Add(deleteMenuItem);
ctxMenuStripRightClick.Items.Add(editMenuItem);
ctxMenuStripRightClick.Items.Add(separatorMenuItem);
ctxMenuStripRightClick.Items.Add(markDoneMenuItem);
}
catch (Exception ex)
{
HandleException.Show(ex);
}
}
-
2\$\begingroup\$ Do you need the intermediate tool menu variables? Could you not just use ctxMenuStripRightClick.Items.Add(new ToolStripMenuItem("Add", null, AddMenuItem_Click)) etc \$\endgroup\$dreza– dreza2013年04月21日 20:42:54 +00:00Commented Apr 21, 2013 at 20:42
-
\$\begingroup\$ Good idea. Is there a way I can avoid repeatedly mentioning ctxMenuStripRightClick ? \$\endgroup\$Kirsten– Kirsten2013年04月21日 21:29:12 +00:00Commented Apr 21, 2013 at 21:29
-
\$\begingroup\$ Yes, see my answer as a bit much for a comment \$\endgroup\$dreza– dreza2013年04月22日 00:44:29 +00:00Commented Apr 22, 2013 at 0:44
2 Answers 2
Is there a way I can avoid repeatedly mentioning ctxMenuStripRightClick.
Yes, I guess you could do something like:
ctxMenuStripRightClick = new ContextMenuStrip();
ctxMenuStripRightClick.Opening += ctxMenuStrip_Opening;
ctxMenuStripRightClick.Items.AddRange(new List<ToolStripMenuItem>
{
new ToolStripMenuItem("Add", null, AddMenuItem_Click),
new ToolStripMenuItem("Delete", null, DeleteMenuItem_Click),
// etc
}
I think you should create new function to add item
private void AddMenuItem(string caption, EventHandler onClick) {
ctxMenuStripRightClick.Items.Add(
new ToolStripMenuItem(caption, null, onClick)
);
}
So your initialization method will be
ctxMenuStripRightClick = new ContextMenuStrip();
ctxMenuStripRightClick.Opening += ctxMenuStrip_Opening;
AddMenuItem("Add", AddMenuItem_Click);
AddMenuItem("Delete", DeleteMenuItem_Click);
AddMenuItem("Edit", EditMenuItem_Click);
ctxMenuStripRightClick.Items.Add(new ToolStripSeparator());
AddMenuItem("Mark Done", MarkDoneMenuItem_Click);
EDIT: If you really don't want a separated function, you can use lambda expression:
Action<string, EventHandler> addMenuItem = (caption, onClick) =>
{
ctxMenuStripRightClick.Items.Add(
new ToolStripMenuItem(caption, null, onClick)
);
};
addMenuItem("Add", AddMenuItem_Click);
...
-
\$\begingroup\$ i like the separate function idea, but I want ctxMenuStripRightClick to be private to the main procedure \$\endgroup\$Kirsten– Kirsten2013年04月22日 15:48:17 +00:00Commented Apr 22, 2013 at 15:48
-
\$\begingroup\$ @kirsteng See my update. \$\endgroup\$tia– tia2013年04月25日 05:02:29 +00:00Commented Apr 25, 2013 at 5:02