[Mono-winforms-list] Help fixing bug 661753 (cloning ToolStripItem objects)

stifu at free.fr stifu at free.fr
Fri Dec 31 12:18:40 EST 2010


See bug 661753: TreeNode clones aren't complete, some properties are missing.

This is the code I have so far (in TreeNode.cs):


		public virtual object Clone()
		{
			TreeNode tn = (TreeNode)Activator.CreateInstance (GetType ());
			tn.name = name;
			tn.text = text;
			tn.image_key = image_key;
			tn.image_index = image_index;
			tn.selected_image_index = selected_image_index;
			tn.selected_image_key = selected_image_key;
			tn.state_image_index = state_image_index;
			tn.state_image_key = state_image_key;
			if (nodes != null) {
				foreach (TreeNode child in nodes)
					tn.nodes.Add ((TreeNode)child.Clone ());
			}
			if (context_menu != null) {
			    tn.context_menu = new ContextMenu();
				foreach (MenuItem item in context_menu.menu_items)
				    tn.context_menu.menu_items.Add (item.CloneMenu ());
			}
			if (context_menu_strip != null) {
			    tn.context_menu_strip = new ContextMenuStrip();
				//foreach (ToolStripItem item in context_menu_strip.Items)
				//    tn.context_menu_strip.Items.Add (item.Clone ());
			}
			tn.tag = tag;
			tn.check = check;
			tn.tool_tip_text = tool_tip_text;
			if (prop_bag != null)
				tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
			return tn;	
		}


However, the problem is the commented lines for context_menu_strip: I can't clone the ToolStripItem objects, as it has no clone method.
So, what would be the best way to clone the ToolStripItem objects? Do I have to copy each property manually (as in here: http://blogs.msdn.com/b/jfoscoding/archive/2005/09/28/475177.aspx)? Hopefully not... If I have to, maybe I should make it an internal method inside ToolStripItem, so other objects can reuse it...?

Thanks for the help.

PS: the original code used both private fields and public properties, but mostly private fields, so I homogenized the whole thing and used private fields everywhere (as I don't think it's necessary to run the property code in this case). Maybe I should have used public properties instead... *shrugs*


More information about the Mono-winforms-list mailing list