C# - TreeView construction -


I'm trying to create a treeview from a menu. My code is like this:

  Public class menuexclusion availability {public triviaview (menmanu menu menu) {tree view tree view = new tree view; Trionode Menonod = New Triode (); Foreign currency (MenuItem mi in the main menu menu) {System.Diagnostics.Debug.WriteLine (mi.Text); MainNode.Text = mi.Text; TriNode TN = Menuitemtrasvers (miles); MainNode.Nodes.Add (Tamilnadu); } TreeView.Nodes.Add (mainNode); See Return Tree; } Private TreeNode MenuTemplates (menu item menu) {tree node tree node = new triode (); Foreach (menuItem.MenuItems in menuItem mile) {System.Diagnostics.Debug.WriteLine (mi.Text); Paid node. Text = mile Text; TreeNode T = Menu Itamatravers (mi); If (tr! = Null & tr.Text! = "") {TreeNode.Nodes.Add (tr); }} Return tree node; }}  

But that does not work.

What could be the problem?

I think there are two problems in the way I should come menu item travers method Start as input you get a menu you declare the variable TreeNode , and assign a new TreeNode frequency for this . Then you loop on the sub item of the menu item. For each recurrence, you specify the text from the sub-item in TreeNode (I think that you come to this TreeNode Want the text of the menu item). To get the desired behavior, you should remove this line from the loop:

  treeNode.Text = mi.Text;  

... and add this line before the loop:

  treeNode.Text = menuItem.Text;  

It seems that you have the same problem in the menu retrieval method, so do the same change there. I think this will solve for you (not tested the code yet, maybe a few missed).

Update

I gave it a bit though, because I thought the code can be a bit simpler, and that's what I came up with Was there. Instead of taking two different methods for main menu and menu input, it brings one process to the same method, as well as a treeEnodeCalue , which means that you can inject the menu structure in the already existing (and populated) TreeView control at any level in the tree. Public class menu application {public static zero menu item traversor (tree node compilation parent collection, menu. menu item menu item} {foreach (menu item in menu item) {System.Diagnostics.Debug.WriteLine (mi. Text); Tree node menuItmode = Parent Collection. Add (mile text); If (mi.MenuItems.Count> 0) {MenuItemTraverse (menuItemNode.Nodes, mi.MenuItems); }}}}

Usage example:

  treeView1.Nodes.Clear (); MenuExtractionUtility.MenuItemTraverse (Tree View 1. Nodes, Main Menu 1. Menu Item);  

This code was placed together soon, so you want to "stabilize" by adding blank check and similar.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -