1
\$\begingroup\$

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);
 }
 }
Mathieu Guindon
75.5k18 gold badges194 silver badges467 bronze badges
asked Apr 21, 2013 at 20:31
\$\endgroup\$
3
  • 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\$ Commented Apr 21, 2013 at 20:42
  • \$\begingroup\$ Good idea. Is there a way I can avoid repeatedly mentioning ctxMenuStripRightClick ? \$\endgroup\$ Commented Apr 21, 2013 at 21:29
  • \$\begingroup\$ Yes, see my answer as a bit much for a comment \$\endgroup\$ Commented Apr 22, 2013 at 0:44

2 Answers 2

3
\$\begingroup\$

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
}
answered Apr 22, 2013 at 0:39
\$\endgroup\$
1
\$\begingroup\$

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);
...
answered Apr 22, 2013 at 13:11
\$\endgroup\$
2
  • \$\begingroup\$ i like the separate function idea, but I want ctxMenuStripRightClick to be private to the main procedure \$\endgroup\$ Commented Apr 22, 2013 at 15:48
  • \$\begingroup\$ @kirsteng See my update. \$\endgroup\$ Commented Apr 25, 2013 at 5:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.