SIGN UP MEMBER LOGIN:    
ARTICLE

Using Treeview & Panel control in Windows Form

Posted by Dorababu M Articles | Windows Controls C# January 17, 2011
This is a sample article, as I have seen many asking regarding how to work with panels I will just show you a sample.
Reader Level:
Download Files:
 

This is a sample article, as I have seen many asking regarding how to work with panels I will just show you a sample

In this article I would like to share with you how you can load a panel corresponding to the selected node from a tree view.

I have done this by using tree view so that it is easy to understand and later you will know how to load user controls to be displayed in a panel. Only one Panel was used and was used in a clear fashion; you can add as many controls as you can and can display for a basic tree view; I have used just a label. I have seen many of them asking that they are using 3 Panels it is a bit tricky to hide the 2 remaining panels and showing a panel. So better go through the example so that you can know how to use a single panel instead of having multiple panels.

Controls Used

Tree view, Panel, User controls.

First, place a tree view and a Panel on the form.

Create the required user controls that you need to be loaded inside the panel.

Sample Forms:

1.gif
 
If User control1 selected

2.gif 

If the latter is selected
 
3.gif

If root was selected after User controls

4.gif 

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Samplapp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Text == "Usercontrol1")
            {
                panel1.Controls.Clear();
                panel1.Visible = true;
                UserControl1 usr1 = new UserControl1();
                usr1.Show();
                panel1.Controls.Add(usr1); 
            }
            if (treeView1.SelectedNode.Text == "Usercontrol2")
            {
                panel1.Controls.Clear();
                panel1.Visible = true;
                UserControl2 usr2 = new UserControl2();
                usr2.Show();
                panel1.Controls.Add(usr2);
            }
            if (treeView1.SelectedNode.Text == "Root")
            {
                panel1.Controls.Clear();
                panel1.Visible = false;
            }
        }
    }
}

Happy Coding!

Login to add your contents and source code to this article
share this article :
post comment
 

nice and simple!

Posted by Clive Bixby May 11, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor