.net 4.0 - C# - Iterating through repeated context menu items with foreach loops -


i'm working on small project requires repetition in context menu items. menu starts right click context menu , branches out different colours, , objects. depending on colour , object selected, object on form change. it's pretty messy, unfortunately can't see easier way of getting done (and thankfully needs done 1 menu).

i've created colours menu items , added them, each colour repeats same 8 objects such:

foreach (menuitem in colors.menuitems) {     menuitem 1 = new menuitem();     one.text = "one";     menuitem 2 = new menuitem();     two.text = "two";     menuitem 3 = new menuitem();     three.text = "three";     menuitem 4 = new menuitem();     four.text = "four";     menuitem 5 = new menuitem();     five.text = "five";     menuitem 6 = new menuitem();     six.text = "six";     menuitem 7 = new menuitem();     seven.text = "seven";     menuitem 8 = new menuitem();     eight.text = "eight";     i.menuitems.add(one);     i.menuitems.add(two);     i.menuitems.add(three);     i.menuitems.add(four);     i.menuitems.add(five);     i.menuitems.add(six);     i.menuitems.add(seven);     i.menuitems.add(eight); } 

first things first, figured it's inefficient have object menu items being created inside foreach loop, moved them before it. when did this, objects added final colour, instead of of them. seemed rather strange , i'd appreciate if explain why occurrs.

secondly, plan change object that's been right clicked based on menu selection person selects. there easier way menu item selected other having menu.click event each individual menu item?

any appreciated.

thanks in advance, bob

first things first, figured it's inefficient have object menu items being created inside foreach loop, moved them before it. when did this, objects added final colour, instead of of them. seemed rather strange , i'd appreciate if explain why occurrs.

each menuitem can child of 1 object. framework silently making child of last thing assigned child of.

much repetition can removed code taking approach similar 1 in alex siepman's solution. i've added click handler , made other small changes.

void something() {     mycoloredobject o = //something     var textvalues = new[] {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight"};      foreach (menuitem color in colors.menuitems)     {         (int = 1; <= 8; i++)         {             menuitem newitem = new menuitem();             newitem.text = textvalues[i];             newitem.click += numberclickhandler(o, color, i);             color.menuitems.add(newitem);         }     } } eventhandler numberclickhandler(mycoloredobject o, menuitem color, int num) {     return (s, e) =>     {         // assign color o identified color , num     }; } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -