How to: Adding and Removing Nodes
Create a ControlTreeNode using one of the three constructors
// Constructor 1: Creates a new ControlTreeNode containing a label with the text
"New Label"
ControlTreeNode controlTreeNode1 = new ControlTreeNode();
// Constructor 2: Creates a new ControlTreeNode containing a label with the text
"My Label"
ControlTreeNode controlTreeNode2 = new ControlTreeNode("My Label");
// Constructor 3: Creates a new ControlTreeNode containing a Button
Button b = new Button();
b.Text = "Click me!";
ControlTreeNode controlTreeNode3 = new ControlTreeNode(b); |
Add node by using the Add() method
// Add root node to ControlTreeView 'controlTreeView1'
controlTreeView1.Nodes.Add(controlTreeNode1);
// Add another root node to ControlTreeView 'controlTreeView1'
controlTreeView1.Nodes.Add(controlTreeNode2);
// Add Child node to ControlTreeNode 'controlTreeNode1'
controlTreeNode1.Nodes.Add(controlTreeNode3);
|
Remove nodes by using the Clear(), Remove() or
RemoveAt() methods
//Remove all Nodes from treeview 'controlTreeView1'
controlTreeView1.Nodes.Clear();
// Remove all child nodes from node 'controlTreeNode1'
controlTreeNode1.Nodes.Clear();
//Remove first root node from ControlTreeView 'controlTreeView1'
controlTreeView1.Nodes[0].Remove();
//or
controlTreeView1.Nodes.RemoveAt(0);
|
|
How to: Adding and removing controls to a ControlTreeNode
Step 1. Create controls
ComboBox cb = new ComboBox();
cb.Items.Add("Option 1");
cb.Items.Add("Option 2");
cb.Items.Add("Option 3");
CheckedListBox
clb = new CheckedListBox();
clb.Items.Add("List Item 1", false);
clb.Items.Add("List
Item 2", true);
clb.Items.Add("List Item 3", true); |
Step2. Create a ControlTreeNode to hold the Controls
// Assign first control to node when creating ControlTreeNode
ControlTreeNode controlTreeNode1 = new ControlTreeNode(cb);
//Add another Control to ControlTreeNode 'controlTreeNode1'
controlTreeNode1.Controls.Add(clb);
//Set Controls layout mode
controlTreeNode1.FlowDirection = FlowDirection.LeftToRight;
//Add 'controlTreeNode1' to ControlTreeView 'controlTreeView1'
controlTreeView1.Nodes.Add(controlTreeNode1);
|
Remove controls by using the Clear(), Remove(Control
value) or
RemoveAt() methods
//Removing all Controls from a ControlTreeNode
controlTreeNode1.Controls.Clear();
//Removing first Control from a ControlTreeNode
controlTreeNode1.Controls.RemoveAt(0);
//Remove ComboBox from ControlTreeNode 'controlTreeNode1'
controlTreeNode1.Controls.Remove(cb); |
|
How to: Determine which tree node was selected
Which ControlTreeNode is currently selected is stored in the ControlTreeView.SelectedNode
Property. The Property's value changes when the user Clicks on a node's
border. The selectionborder width (top, bottom, left and right) can be set using
the ControlTreeView.NodeSelectBorderWidth
property. This value is inherited to all new
nodes added to the Treeview. This default value can be changed for individual nodes
by using the ControlTreeNode.SelectBorderWidth
property.
Note: Setting the selection
border width to 0 will disable the Controls automatic node selection functionality.
In this case, you will have to select nodes programmatically by reacting to other
events e.g. clicked event of a control within the tree node.
Using the ControlTreeView.SelectedNode Property
//Use the ControlTreeView.SelectedNode
Property to determine which Node is selected
ControlTreeNode selectedNode
= controlTreeView1.SelectedNode;
//or to set the SelectedNode programatically
controlTreeView1.SelectedNode = controlTreeView1.Nodes[0]; |
Events ControlTreeView.BeforeSelect and ControlTreeView.AfterSelect
//Use the ControlTreeView.BeforeSelect and ControlTreeView.AfterSelect
events to determine which two nodes are involved in the selection change.
ControlTreeNode
selectedNodeBeforeChange;
ControlTreeNode selectedNodeAfterChange;
controlTreeView1.BeforeSelect
+= new EventHandler(controlTreeView1_BeforeSelect);
controlTreeView1.AfterSelect
+= new EventHandler(controlTreeView1_AfterSelect);
void controlTreeView1_BeforeSelect(object
sender, EventArgs e)
{
//values of selectedNodeBeforeChange and selectedNodeAfterChange
are different.
selectedNodeBeforeChange = controlTreeView1.SelectedNode;
selectedNodeAfterChange = (ControlTreeNode)sender;
}
void controlTreeView1_AfterSelect(object sender, EventArgs
e)
{
//values of selectedNodeBeforeChange and selectedNodeAfterChange are the same.
selectedNodeBeforeChange = controlTreeView1.SelectedNode;
selectedNodeAfterChange = (ControlTreeNode)sender;
} |
|
How to: Implement Drag and Drop
Drag and Drop operations are implemented using the following
3 events
- controlTreeView1.NodeMouseDown
- controlTreeView1.NodeDragEnter
- controlTreeView1.NodeDragDrop
The following examples allows the user to move nodes within
the tree using drag and drop.
// Subscribe to events
controlTreeView1.NodeMouseDown += new MouseEventHandler(controlTreeView1_NodeMouseDown);
controlTreeView1.NodeDragEnter += new DragEventHandler(controlTreeView1_NodeDragEnter);
controlTreeView1.NodeDragDrop += new DragEventHandler(controlTreeView1_NodeDragDrop);
void controlTreeView1_NodeMouseDown(object sender, MouseEventArgs e)
{
//Start Drag and Drop operation when a user clicks on the node's selectionborder
controlTreeView1.DoDragDrop(sender, DragDropEffects.Move);
}
void controlTreeView1_NodeDragEnter(object sender, DragEventArgs e)
{
//Make sure that a ControlTreeNode object is being draged over the
destination node.
if (e.Data.GetDataPresent("Tawil.Windows.Controls.ControlTreeNode"))
{
// Determine which node is being moved
ControlTreeNode dragSource = (ControlTreeNode)e.Data.GetData("Tawil.Windows.Controls.ControlTreeNode");
// Determine which node is the new parent
ControlTreeNode dragDestination = (ControlTreeNode)sender;
// Make sure that source and destination are different
if (dragSource == dragDestination)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
void controlTreeView1_NodeDragDrop(object sender, DragEventArgs e)
{
try
{
//Make sure that a ControlTreeNode object
is being droped over the destination node.
if (e.Data.GetDataPresent("Tawil.Windows.Controls.ControlTreeNode"))
{
// Determine
which node is being moved
ControlTreeNode dragSource = (ControlTreeNode)e.Data.GetData("Tawil.Windows.Controls.ControlTreeNode");
// Determine which node is the new
parent
ControlTreeNode dragDestination
= (ControlTreeNode)sender;
// Make sure that source and destination
are different
if (dragSource
!= dragDestination)
{
// Move node
dragSource.ChangeParentNode(dragDestination);
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
} |
|