Windows Develop Bookmark and Share   
 index > Windows Forms General > tree view
 

tree view

hi,

i have 40 forms in my project.i stored the form name and formid in a table.What i want is while loading my application i want to list this all forms name in a tree view model under one parent.And while clicking i want to load that particular form.Any body can send the code for that

thanks elby
  • Moved byeryangMSFTWednesday, September 23, 2009 2:25 AM (From:.NET Base Class Library)
  •  
Elby Paul  Tuesday, September 22, 2009 5:31 AM
By the way you can bind your tree view using this code with data base:
private TreeView tView = new TreeView();
public void PopulateTree()
{
DataTable dt = new DataTable();
Hashtable ht = new Hashtable();

SqlConnection conn = new SqlConnection("data source=tstsvr;initial catalog=TreeTest;integrated security=SSPI;");

new SqlDataAdapter("SELECT * FROM Nodes ORDER BY NodeLevel Asc", conn).Fill(dt);

foreach (DataRow r in dt.Rows)
{
int nodeLvl = int.Parse(r["NodeLevel"].ToString());
string nodeParent = r["Parent"].ToString();
string nodeName = r["Name"].ToString();

TreeNode tNode = new TreeNode(nodeName);
ht.Add(nodeLvl.ToString() + nodeName, tNode);

if (tView.Nodes.Count == 0)
tView.Nodes.Add(tNode);
else
{
nodeLvl --;
((TreeNode) ht[nodeLvl.ToString() + nodeParent]).Nodes.Add(tNode);
}
}
ht.Clear();
}
Thanks,
Varun Kumar
varun007  Tuesday, September 22, 2009 5:59 AM
why you are storing forms in data base? use Tree view control directly and give your form's name there. on click u can load forms from there.
Varun Kumar
varun007  Tuesday, September 22, 2009 5:42 AM
but the forms are not predefined
Elby Paul  Tuesday, September 22, 2009 5:48 AM
Please explain the structure ofyour application first.
Kind regards,
Varun Kumar
varun007  Tuesday, September 22, 2009 5:54 AM
By the way you can bind your tree view using this code with data base:
private TreeView tView = new TreeView();
public void PopulateTree()
{
DataTable dt = new DataTable();
Hashtable ht = new Hashtable();

SqlConnection conn = new SqlConnection("data source=tstsvr;initial catalog=TreeTest;integrated security=SSPI;");

new SqlDataAdapter("SELECT * FROM Nodes ORDER BY NodeLevel Asc", conn).Fill(dt);

foreach (DataRow r in dt.Rows)
{
int nodeLvl = int.Parse(r["NodeLevel"].ToString());
string nodeParent = r["Parent"].ToString();
string nodeName = r["Name"].ToString();

TreeNode tNode = new TreeNode(nodeName);
ht.Add(nodeLvl.ToString() + nodeName, tNode);

if (tView.Nodes.Count == 0)
tView.Nodes.Add(tNode);
else
{
nodeLvl --;
((TreeNode) ht[nodeLvl.ToString() + nodeParent]).Nodes.Add(tNode);
}
}
ht.Clear();
}
Thanks,
Varun Kumar
varun007  Tuesday, September 22, 2009 5:59 AM
Hi,
may be it will help you.
 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
           switch(e.Node.Text)
           {
               case "Form2":
                   Form2 frm2 = new Form2();
                   frm2.Show();                   
                   break;
               default:
                   break;

           }
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Tuesday, September 22, 2009 6:24 AM

You can use google to search for other answers

Custom Search

More Threads

• Advanced docking.
• DATAGridview Suggestion???
• Stack Overflow
• Communicating between objects in object model
• c# listview - add items and subitems only by condition
• WinForms app after conversion to Orcas
• Working with multiple forms
• Currency formatting problem with DataGridTextBoxColumn?
• Bascule between the Languages Keyboards
• Web Browser Favorites List