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);}}