C # Treeview

Source: Internet
Author: User

C # basic operations on contacts of the windows from control Treeview: Add new contacts, add sibling contacts, and delete contacts;
The implementation code is as follows:

Delete {treeView1.SelectedNode. Remove ()};

Add contacts:
Private void AddChildNode ()
{
If (treeView1.SelectedNode = null)
{
MessageBox. Show ("select a node", "message", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
// Create a Node object and initialize it
Try
{
ArrayList r = new ArrayList ();
Form3 f = new Form3 ();
F. ShowDialog (this );
TreeNode tmp = new TreeNode (f. textBox1.Text );
R. Add (f. textBox1.Text. ToString ());
R. Add (f. textBox2.Text. ToString ());
R. Add (f. textBox3.Text. ToString ());
R. Add (f. textBox4.Text. ToString ());
R. Add (f. textBox5.Text. ToString ());
R. Add (f. richTextBox1.Text. ToString ());
Tmp. Tag = r;
Tmp. Text = f. textBox1.Text;
ArrayList t = (ArrayList) this. treeView1.SelectedNode. Tag;
If (System. Convert. ToDateTime (f. textBox2.Text. ToString () <= System. Convert. ToDateTime (t [1]. ToString ()))
{
MessageBox. Show ("How is it possible! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
TreeView1.SelectedNode. Nodes. Add (tmp );
TreeView1.SelectedNode = tmp;
TreeView1.ExpandAll ();
}
}
Catch
{
MessageBox. Show ("Date entered correctly! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}

}
Private void AddParent ()
{
// First determine whether to select the node location in the component
If (treeView1.SelectedNode. Parent = null)
{
MessageBox. Show ("select a node", "message", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
Try
{
ArrayList r = new ArrayList ();
Form3 f = new Form3 ();
F. ShowDialog ();
R. Add (f. textBox1.Text. ToString ());
R. Add (f. textBox2.Text. ToString ());
R. Add (f. textBox3.Text. ToString ());
R. Add (f. textBox4.Text. ToString ());
R. Add (f. textBox5.Text. ToString ());
R. Add (f. richTextBox1.Text. ToString ());
TreeNode tmp = new TreeNode (f. textBox1.Text );
Tmp. Tag = r;
Tmp. Text = f. textBox1.Text;
ArrayList t = (ArrayList) this. treeView1.SelectedNode. Parent. Tag;
If (System. Convert. ToDateTime (f. textBox2.Text) <= System. Convert. ToDateTime (t [1]. ToString ()))
{
MessageBox. Show ("How is it possible! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
TreeView1.SelectedNode. Parent. Nodes. Add (tmp );
TreeView1.ExpandAll ();
}
}
Catch
{
MessageBox. Show ("Date entered correctly! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
// Add a sibling node to the TreeView component
}
}
Traversal Algorithm:
Public TreeNode FindNode (TreeNode root, string strValue)
{
If (root = null)
Return null;
If (root. Text = strValue)
Return root;
TreeNode r = null;
Foreach (TreeNode node in root. Nodes)
{
R = FindNode (node, strValue );
If (r! = Null)
Break;
}
Return r;
}

Serialization to store TREEVIEW:
/// Class2 serializes the TreeView
/// Used for saving and reading files;
/// Write the file in binary format
Public class TreeViewDataAccess
{

/// TreeViewData
[Serializable ()]
Public struct TreeViewData
{
Public TreeNodeData [] Nodes;

/// Recursively initialize the TreeView data
Public TreeViewData (TreeView treeview)
{
Nodes = new TreeNodeData [treeview. Nodes. Count];
If (treeview. Nodes. Count = 0)
{
Return;
}
For (int I = 0; I <= treeview. Nodes. Count-1; I ++)
{
Nodes = new TreeNodeData (treeview. Nodes );
}
}
/// Use TreeViewData to pop up the TreeView
Public void PopulateTree (TreeView treeview)
{
If (this. Nodes = null | this. Nodes. Length = 0)
{
Return;
}
Treeview. BeginUpdate ();
For (int I = 0; I <= this. Nodes. Length-1; I ++)
{
Treeview. Nodes. Add (this. Nodes. ToTreeNode ());
}
Treeview. EndUpdate ();
}
}

/// TreeNodeData
[Serializable ()]
Public struct TreeNodeData
{
Public string Text;
Public int ImageIndex;
Public int SelectedImageIndex;
Public bool Checked;
Public bool Expanded;
Public object Tag;
Public Color BackColor;
Public TreeNodeData [] Nodes;
/// TreeNode Constructor
Public TreeNodeData (TreeNode node)
{
This. Text = node. Text;
This. ImageIndex = node. ImageIndex;
This. SelectedImageIndex = node. SelectedImageIndex;
This. Checked = node. Checked;
This. BackColor = node. BackColor;
This. Expanded = node. IsExpanded;
This. Nodes = new TreeNodeData [node. Nodes. Count];
This. Tag = node. Tag;
If (node. Nodes. Count = 0)
{
Return;
}
For (int I = 0; I <= node. Nodes. Count-1; I ++)
{
Nodes = new TreeNodeData (node. Nodes );
}
}
/// TreeNodeData returns TreeNode
Public TreeNode ToTreeNode ()
{
TreeNode ToTreeNode = new TreeNode (this. Text, this. ImageIndex, this. SelectedImageIndex );
ToTreeNode. Checked = this. Checked;
ToTreeNode. BackColor = this. BackColor;
ToTreeNode. Tag = this. Tag;
If (this. Expanded)
{
ToTreeNode. Expand ();
}
If (this. Nodes = null & this. Nodes. Length = 0)
{
Return null;
}
If (ToTreeNode! = Null & this. Nodes. Length = 0)
{
Return ToTreeNode;
}
For (int I = 0; I <= this. Nodes. Length-1; I ++)
{
ToTreeNode. Nodes. Add (this. Nodes. ToTreeNode ());
}
Return ToTreeNode;
}
}
/// Load the TreeView
Public static void LoadTreeViewData (TreeView treeView, string path)
{
Try
{
BinaryFormatter ser = new BinaryFormatter ();
Stream file = new FileStream (path, FileMode. Open, FileAccess. Read, FileShare. Read );
TreeViewData treeData = (TreeViewData) (ser. Deserialize (file )));
TreeData. PopulateTree (treeView );
File. Close ();
}
Catch
{}
}
/// Save the TreeView to the file
Public static void SaveTreeViewData (TreeView treeView, string path)
{
Try
{
BinaryFormatter ser = new BinaryFormatter ();
Stream file = new FileStream (path, FileMode. Create );
Ser. Serialize (file, new TreeViewData (treeView ));
File. Close ();
}
Catch
{
}
}
}
}

Related Article

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.