Custom TreeNode attributes

Source: Internet
Author: User

I. purpose: to customize the TreeNode attribute so that the control can store more attributes. Conducive to development
Ii. Method:

Public class GroupTreeNode: TreeNode
{
Private IPAddress ip address;
Private HostStatus status;
Public HostStatus Status
{
Set {status = value ;}
Get {return status ;}
}
Public IPAddress IP
{
Set {ip = value ;}
Get {return ip ;}
}

Public GroupTreeNode ()
: Base ()
{

}
Public GroupTreeNode (string text, int imageIndex, int selectedImageIndex, IPAddress address)
: Base (text, imageIndex, selectedImageIndex)
{
This. ip = address;
}
}

Iii. Application

Code
TreeNode rootNode;
TreeNode channelNode;
GroupTreeNode groupTreeNode;
String SQL = "select sysname from sysconfig ";
CMySql mydb = new CMySql ();
DataTable dt = new DataTable ();
Dt = mydb. SelectDt (SQL );
Foreach (DataRow row in dt. Rows)
{
Foreach (DataColumn column in dt. Columns)
{
String mss = String. Format ("{0}", row [column]);
RootNode = treeVideo_server.AddRoot (mss, null );
String sql2 = "select servername, ipaddress from serverconfig where sysname = '" + mss + "'";

CMySql db2 = new CMySql ();
DataTable dt2 = new DataTable ();
Dt2 = db2.SelectDt (sql2 );
Foreach (DataRow row2 in dt2.Rows)
{
String name = row2 ["servername"]. ToString ();
IPAddress ip = IPAddress. Parse (row2 ["ipaddress"]. ToString ());
GroupTreeNode = treeVideo_server.AddGroup (name, rootNode, ip); // attributes with ip addresses

String sql3 = "select channelname from channelconfig where servername = '" + name + "'";
CMySql db3 = new CMySql ();
DataTable dt3 = db3.SelectDt (sql3 );

Foreach (DataRow row3 in dt3.Rows)
{
Foreach (DataColumn column3 in dt3.Columns)
{
String mss3 = String. Format ("{0}", row3 [column3]);
ChannelNode = treeVideo_server.AddChannel (mss3, groupTreeNode );
}
}
}
}

4. Others
Cyclically access all nodes of the TreeView Control

private void PrintRecursive(TreeNode treeNode){// Print the node.System.Diagnostics.Debug.WriteLine(treeNode.Text);MessageBox.Show(treeNode.Text);// Print each node recursively.foreach (TreeNode tn in treeNode.Nodes){PrintRecursive(tn);}}// Call the procedure using the TreeView.private void CallRecursive(TreeView treeView){// Print each node recursively.TreeNodeCollection nodes = treeView.Nodes;foreach (TreeNode n in nodes){PrintRecursive(n);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.