• 0

[C#] TreeView Question


Question

How do you add child nodes to a specific node on a TreeView? I can't seem to figure out how to do it at all... I keep getting either a linear list or an unhandled exceptions about no current node selected.

Thanks,

- Mark

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
How do you add child nodes to a specific node on a TreeView? I can't seem to figure out how to do it at all... I keep getting either a linear list or an unhandled exceptions about no current node selected.

Thanks,

- Mark

584822369[/snapback]

How are you doing it now?

From MSDN:

// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList(); 

private void FillMyTreeView()
{
   // Add customers to the ArrayList of Customer objects.
   for(int x=0; x<1000; x++)
   {
      customerArray.Add(new Customer("Customer" + x.ToString()));
   }

   // Add orders to each Customer object in the ArrayList.
   foreach(Customer customer1 in customerArray)
   {
      for(int y=0; y<15; y++)
      {
         customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));    
      }
   }

   // Display a wait cursor while the TreeNodes are being created.
   Cursor.Current = new Cursor("MyWait.cur");
        
   // Suppress repainting the TreeView until all the objects have been created.
   treeView1.BeginUpdate();

   // Clear the TreeView each time the method is called.
   treeView1.Nodes.Clear();

   // Add a root TreeNode for each Customer object in the ArrayList.
   foreach(Customer customer2 in customerArray)
   {
      treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
          
      // Add a child treenode for each Order object in the current Customer object.
      foreach(Order order1 in customer2.CustomerOrders)
      {
         treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
           new TreeNode(customer2.CustomerName + "." + order1.OrderID));
      }
   }

   // Reset the cursor to the default for all controls.
   Cursor.Current = Cursors.Default;

   // Begin repainting the TreeView.
   treeView1.EndUpdate();
}

Link to comment
Share on other sites

  • 0

Well I have a method:

public void getDiskInfo() {
  
 	 long megabyteByteLength = (1024 * 1024); //total number of bytes in one megabyte

 	 objectQuery = new ObjectQuery("SELECT Name, Size, FreeSpace FROM Win32_LogicalDisk WHERE DriveType = 3");
 	 ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(objectManagementScope, objectQuery);
 	 ManagementObjectCollection objectReturnCollection = objectSearcher.Get();

 	 treeControl.Nodes.Add("Hard Disk");

 	 foreach( ManagementObject objectReturn in objectReturnCollection ) {

    treeControl.Nodes.Add("Name: " + objectReturn["Name"].ToString());

 	 //	MessageBox.Show("Size: {0:#,###} MB", Convert.ToInt64(objectReturn["Size"]) / megabyteByteLength);
 	 //	MessageBox.Show("Free: {0:#,###} MB", Convert.ToInt64(objectReturn["FreeSpace"]) / megabyteByteLength);
 	 //	MessageBox.Show("> Used: {0:#,###} MB", (Convert.ToInt64(objectReturn["Size"]) - Convert.ToInt64(objectReturn["FreeSpace"])) / megabyteByteLength);
 	 }
  }

So basically what I want is a structure in my TreeView that will look like this:

|-> Hard Disk
|       |
|       |-> Name: Mark's Disk C:

Rather than:

|-> Hard Disk
|-> Name: Mark's Disk C:

So the code:

treeControl.Nodes.Add("Name: " + objectReturn["Name"].ToString());

... should be changed so the node is being added as a child node to the "Hard Disk" node.

Link to comment
Share on other sites

  • 0

treeControl.Nodes.Add("Hard Disk");

Take that out and use this instead:

TreeNode mark = new TreeNode("Mark's Disk C:");
TreeNode HD = new TreeNode("Hard Disk");
HD.Nodes.Add(mark);
treeControl.Nodes.Add(HD);

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.