Treeview ExpandAll on Load


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Treeview ExpandAll on Load

  1. #1
    Join Date
    Jul 2007
    Posts
    7

    Treeview ExpandAll on Load

    Hi

    I am using a treeview in my windows form and having trouble expanding it by default. I have placed a treeview control on a panel and programatically added/loaded the tree nodes using
    public frmDashBoard()
    {
    InitializeComponent();
    LoadTreeview();
    }
    Treeview looks like..
    +ParentNode
    +Child1
    +Child2
    Issue is that after I populate the treeview I am not able to expand the treeview at all. I tried putting the following code in every **** place i can think off..
    tvwAttributes.ExpandAll();
    This statement works fine if called from click event of a command button but doesn't work anywhere else. I want the treeview to expand always. Please guide me what am doing wrong.. Thanks in advance!

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    This code works fine for me:
    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace MyApp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                InitializeTreeView();
            }
    
            // Populates a TreeView control with example nodes. 
            private void InitializeTreeView()
            {
                treeView1.BeginUpdate();
                treeView1.Nodes.Add("Parent");
                treeView1.Nodes[0].Nodes.Add("Child 1");
                treeView1.Nodes[0].Nodes.Add("Child 2");
                treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
                treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild");
                treeView1.Nodes.Add("Parent");
                treeView1.Nodes[1].Nodes.Add("Child 1");
                treeView1.Nodes[1].Nodes.Add("Child 2");
                treeView1.Nodes[1].Nodes[1].Nodes.Add("Grandchild");
                treeView1.Nodes[1].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild");
                treeView1.EndUpdate();
                treeView1.ExpandAll();
            }
        }
    }
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jul 2007
    Posts
    7

    Treeview ExpandAll on Load

    Hi Phil

    Thanks for the response.. I guess there is something really worng the way I am coding.. When I try to run your code standalone, it works great. As soon as I place it in a MDIForm it fails to expand automatically.

    Main Program Code
    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    //Application.Run(new CDIM.Tracker.Modules.frmDashboard());
    Application.Run(new TrackerMDI()); //Calling MDI
    }
    }

    MDI Form code
    public TrackerMDI()
    {
    InitializeComponent();

    Form1 objForm1 = new Form1(); //This is the form that has treeview
    objForm1.Visible = true;
    objForm1.MdiParent = this;
    objForm1.Show(); //Shows treeview without expanding any node
    }

    Form1 has the code sent by you.

    What can be the problem with MDIForm???

  4. #4
    Join Date
    Jul 2007
    Posts
    7
    Thanks to everyone for looking into this problem.. I was able to solve this with the help of my friend.

    Only thing that we did in the code is:
    We removed the following statement from MDI Form code public TrackerMDI()
    objForm1.Visible = true;

    Not sure how that impacted but it solved my problem

Similar Threads

  1. Load TreeView Control From Database Table
    By firegate in forum ASP.NET
    Replies: 1
    Last Post: 08-02-2008, 08:31 AM
  2. Replies: 0
    Last Post: 03-24-2008, 09:46 PM
  3. Load tab control using treeview childnode
    By crystalelle in forum .NET
    Replies: 11
    Last Post: 02-22-2008, 03:55 AM
  4. Looking for fast treeview load routines
    By Ron Weller in forum VB Classic
    Replies: 0
    Last Post: 01-31-2007, 12:23 PM
  5. Replies: 7
    Last Post: 02-24-2006, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center