1. How do I
implement Drag and Drop support between two TreeViews?
2. How do I make
the TreeView scroll when I drag an item to the top or bottom?
3. Does TreeView
support multiple selections?
4. How do I
display checkboxes in the nodes of a treeview?
5. How can I get
a tooltip to vary from node to node in my treeview?
6. When I
right-click a tree node, it does not become selected. How can I make it be
selected on a right-click?
7. When I get
the SelectedNode in the treeview's Click event, it is the old selection. How
do I get the newly selected node?
8. How can I
determine the node level of a node in my treeview?
1 How do I
implement Drag and Drop support between two TreeViews?
In a posting in the
Microsoft.Windows.Forms newsgroup, Brian Roder (Microsoft) gives VB.Net code
snippets to handle the DragEnter, ItemDrag and DragDrop events that provide
a solution to this problem. You can get C# code in this sample, TreeViewDnD.
Here is some sample handlers.
private void
treeView2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeNode newNode;
if(
e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point pt;
TreeNode destinationNode;
pt =
treeView2.PointToClient(new Point(e.X, e.Y));
destinationNode =
treeView1.GetNodeAt(pt);
newNode = (TreeNode)
e.Data.GetData("System.Windows.Forms.TreeNode");
if(!destinationNode.Equals(newNode))
{
//destinationNode.Nodes.Add(newNode.Clone());
destinationNode.Nodes.Add((TreeNode)
newNode.Clone());
destinationNode.Expand();
//Remove original
node
newNode.Remove();
}
}
}
private void
treeView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void
treeView2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item,
DragDropEffects.Move);
}
2 How do I
make the TreeView scroll when I drag an item to the top or bottom?
When you drag an item within
the TreeView, you can handle the DragOver event for a drag-drop. If you want
to drag-drop into a spot that's not currently visible, you can scroll the
TreeView by handling the DragOver event:
[C#]
private void
treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeView tv = sender as
TreeView;
Point pt =
tv.PointToClient(new Point(e.X,e.Y));
int delta = tv.Height -
pt.Y;
if ((delta < tv.Height /
2) && (delta > 0))
{
TreeNode tn =
tv.GetNodeAt(pt.X, pt.Y);
if (tn.NextVisibleNode
!= null)
tn.NextVisibleNode.EnsureVisible();
}
if ((delta > tv.Height /
2) && (delta < tv.Height))
{
TreeNode tn =
tv.GetNodeAt(pt.X, pt.Y);
if (tn.PrevVisibleNode
!= null)
tn.PrevVisibleNode.EnsureVisible();
}
}
[VB.NET]
Private Sub
treeView1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs)
If TypeOf sender is TreeView Then
Dim tv As TreeView =
CType(sender, TreeView)
Dim pt As Point =
tv.PointToClient(New Point(e.X, e.Y))
Dim delta As Integer =
tv.Height - pt.Y
If delta < tv.Height / 2 And
delta > 0 Then
Dim tn As TreeNode =
tv.GetNodeAt(pt.X, pt.Y)
If Not (tn.NextVisibleNode
Is Nothing) Then
tn.NextVisibleNode.EnsureVisible()
End If
End If
If delta > tv.Height / 2 And
delta < tv.Height Then
Dim tn As TreeNode =
tv.GetNodeAt(pt.X, pt.Y)
If Not (tn.PrevVisibleNode
Is Nothing) Then
tn.PrevVisibleNode.EnsureVisible()
End If
End If
End If
End Sub 'treeView1_DragOver
3 Does
TreeView support multiple selections?
Brian Roder (Microsoft)
responded to this question in a posting on microsoft.public.dotnet.framework.windowsforms newsgroup.
The managed Treeview control
does not support it because the native Treeview control does not support
multi-selection. I think it can be accomplished through subclassing, however
I am not aware of a managed sample for accomplishing this.
4 How do I
display checkboxes in the nodes of a treeview?
Set the TreeView.CheckBoxes
property to true.
5 How can
I get a tooltip to vary from node to node in my treeview?
Try using a MouseMove event
handler and checking to see if you have moved to a new node, and if so, set
a new tiptext.
[C#]
private int oldNodeIndex = -1;
private ToolTip toolTip1;
private void Form1_Load(object
sender, System.EventArgs e)
{
this.toolTip1 = new
System.Windows.Forms.ToolTip();
this.toolTip1.InitialDelay = 300; //half a second delay
this.toolTip1.ReshowDelay
= 0;
}
private void
treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
TreeNode tn =
this.treeView1.GetNodeAt(e.X, e.Y);
if(tn != null)
{
int currentNodeIndex
= tn.Index;
if(currentNodeIndex
!= oldNodeIndex)
{
oldNodeIndex =
currentNodeIndex;
if(this.toolTip1 != null && this.toolTip1.Active)
this.toolTip1.Active = false; //turn it off
this.toolTip1.SetToolTip(this.treeView1,
string.Format("tooltip: node {0}", oldNodeIndex));
this.toolTip1.Active = true; //make it active so it can
show
}
}
}
[VB.NET]
Private oldNodeIndex As
Integer = - 1
Private toolTip1 As ToolTip
Private Sub Form1_Load(sender
As Object, e As System.EventArgs)
Me.toolTip1 = New
System.Windows.Forms.ToolTip()
Me.toolTip1.InitialDelay
= 300 'half a second delay
Me.toolTip1.ReshowDelay =
0
End Sub 'Form1_Load
Private Sub
treeView1_MouseMove(sender As Object, e As
System.Windows.Forms.MouseEventArgs)
Dim tn As TreeNode =
Me.treeView1.GetNodeAt(e.X, e.Y)
If Not (tn Is Nothing)
Then
Dim currentNodeIndex
As Integer = tn.Index
If currentNodeIndex
<> oldNodeIndex Then
oldNodeIndex =
currentNodeIndex
If Not (Me.toolTip1
Is Nothing) And Me.toolTip1.Active Then
Me.toolTip1.Active = False 'turn it off
End If
Me.toolTip1.SetToolTip(Me.treeView1, String.Format("tooltip:
node {0}", oldNodeIndex))
Me.toolTip1.Active =
True 'make it active so it can show
End If
End If
End Sub 'treeView1_MouseMove
6 When I
right-click a tree node, it does not become selected. How can I make it be
selected on a right-click?
Handle the treeview's
mousedown event, and if it is the right-click, then explicitly set focus to
the node under the click.
7 When I
get the SelectedNode in the treeview's Click event, it is the old selection.
How do I get the newly selected node?
Try using the AfterSelect
event instead of the Click event. The Click event is inherited from
Control.Click and occurs before the new selection is set into SelectedNode.
The AfterSelect event is fired after the newly selected node is placed in
the SelectedNode property. This code illustrates these events.
private void
treeView2_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs
e)
{
TreeNode node =
treeView2.SelectedNode;
Console.WriteLine("AfterSelect:"
+ node.ToString());//from tree
Console.WriteLine("AfterSelect:"
+ e.Node.ToString());//from event args
}
private void treeView2_Click(object
sender, System.EventArgs e)
{
TreeNode node =
treeView2.SelectedNode;
if(node == null)
Console.WriteLine("Click:
(none)");
else
Console.WriteLine("Click:
" + node.ToString());
}
8 How can
I determine the node level of a node in my treeview?
Here is a code snippet
suggested by Mattias Sjgren on the microsoft.public.dotnet.languages.csharp
newsgroup.
[C#]
public int NodeLevel(TreeNode
node)
{
int level = 0;
while ((node =
node.Parent) != null) level++;
return level;
}
[VB.NET]
Public Sub NodateLevel(ByVal
node as TreeNode) As Integer
Dim level as Integer = 0
While Not node Is Nothing
node = node.Parent
level = level + 1
End While
End Sub