:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Windows Forms TreeView

The General Winforms Interview Questions consists the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

23. Windows Forms TreeView

    23.1 How do I implement Drag and Drop support between two TreeViews?
    23.2 How do I make the TreeView scroll when I drag an item to the top or bottom?
    23.3 How do I display checkboxes in the nodes of a treeview?
    23.4 When I right-click a tree node, it does not become selected. How can I make it be selected on a right-click?
    23.5 When I get the SelectedNode in the treeview's Click event, it is the old selection. How do I get the newly selected node?
    23.6 How can I get a tooltip to vary from node to node in my treeview?
    23.7 How can I determine the node level of a node in my treeview?
    23.8 How can I populate a TreeView Control with XML Data?
    23.9 How can I clone/copy all the nodes from one TreeView Control to another?
    23.10 How do I programmatically select a node in a TreeView control?
    23.11 Can I select multiple nodes in a TreeView Control?
    23.12 How can I display a context menu when the user right-clicks on a node in the TreeView control?
    23.13 How can I ensure that a node is selected when the user clicks along the line of a node?

23.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.


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 = treeView2.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);
}

23.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

23.3 How do I display checkboxes in the nodes of a treeview?

Set the TreeView.CheckBoxes property to true.


23.4 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 th enode under the click.
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
treeView1.SelectedNode = treeView1.GetNodeAt (e.X ,e.Y );
}
}

23.5 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)c {
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());
}

23.6 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

23.7 How can I determine the node level of a node in my treeview?

Here is a code snippet suggested by Mattias Sjögren 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

23.8 How can I populate a TreeView Control with XML Data?

The following articles on MSDN give you step by step instructions on how you can populate a TreeView Control with data from an XML file.


23.9 How can I clone/copy all the nodes from one TreeView Control to another?

The following code snippet demonstrates how you can clone or copy all the nodes in TreeView1 to TreeView2 by clicking on Button1.

[C#]
private void IterateTreeNodes (TreeNode originalNode, TreeNode rootNode)
{
foreach( TreeNode childNode in originalNode.Nodes)
{
TreeNode newNode = new TreeNode(childNode.Text);
newNode.Tag = childNode.Tag;
this.treeView2.SelectedNode = rootNode;
this.treeView2.SelectedNode.Nodes.Add(newNode);
IterateTreeNodes(childNode, newNode);
}
}
//Button Click code
private void button1_Click(object sender, System.EventArgs e)
{
foreach( TreeNode originalNode in this.treeView1.Nodes)
{
TreeNode newNode = new TreeNode(originalNode.Text);
newNode.Tag = originalNode.Tag;
this.treeView2.Nodes.Add(newNode);
IterateTreeNodes(originalNode, newNode);
}
}
[VB.NET]
Private Sub IterateTreeNodes(ByVal originalNode As TreeNode, ByVal rootNode As TreeNode)
Dim childNode As TreeNode
For Each childNode In originalNode.Nodes
Dim NewNode As TreeNode = New TreeNode(childNode.Text)
NewNode.Tag = childNode.Tag
Me.treeView2.SelectedNode = rootNode
Me.treeView2.SelectedNode.Nodes.Add(NewNode)
IterateTreeNodes(childNode, NewNode)
Next
End Sub
'Button Click code
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim originalNode As TreeNode
For Each originalNode In Me.treeView1.Nodes
Dim NewNode As TreeNode = New TreeNode(originalNode.Text)
NewNode.Tag = originalNode.Tag
Me.treeView2.Nodes.Add(NewNode)
IterateTreeNodes(originalNode, NewNode)
Next
End Sub

23.10 How do I programmatically select a node in a TreeView control?

You need to set the HideSelection property of the TreeView to false and call:

[C#]
//Select the first node
this.treeView1.SelectedNode = this.treeView1.Nodes[0];
[VB.NET]
'Select the first node
Me.treeView1.SelectedNode = Me.treeView1.Nodes(0)

23.10 Can I select multiple nodes in a TreeView Control?

The Treeview control does not support multiple selections but there are samples posted at GotDotNet which demonstrates how this feature can be implemented.


23.12 How can I display a context menu when the user right-clicks on a node in the TreeView control?

You can display a context menu when a user right-clicks on a node by listening to the TreeView's MouseUp event as shown below:

[C#]
private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
Point ClickPoint = new Point(e.X,e.Y);
TreeNode ClickNode = treeView1.GetNodeAt(ClickPoint);
if(ClickNode == null) return;
// Convert from Tree coordinates to Screen coordinates
Point ScreenPoint = treeView1.PointToScreen(ClickPoint);
// Convert from Screen coordinates to Form coordinates
Point FormPoint = this.PointToClient(ScreenPoint);
// Show context menu
contextmenu.MenuItems.Clear();
contextmenu.MenuItems.Add("Item1");
contextmenu.MenuItems.Add("Item2");
contextmenu.Show(this,FormPoint);
}
}
[VB.NET]
Private Sub treeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X,e.Y)
Dim ClickNode As TreeNode = treeView1.GetNodeAt(ClickPoint)
If ClickNode Is Nothing Then
Return
End If
' Convert from Tree coordinates to Screen coordinates
Dim ScreenPoint As Point = treeView1.PointToScreen(ClickPoint)
' Convert from Screen coordinates to Form coordinates
Dim FormPoint As Point = Me.PointToClient(ScreenPoint)
' Show context menu

contextmenu.MenuItems.Clear()
contextmenu.MenuItems.Add("Item1")
contextmenu.MenuItems.Add("Item2")
contextmenu.Show(this,FormPoint)
End If
End Sub

23.13 How can I ensure that a node is selected when the user clicks along the line of a node?

A click event will be fired but a node will not be selected when the user clicks to the right of a node. This code snippets show how you can ensure that a node is selected in this scenario:

[C#]
private void treeView1_Click(object sender, System.EventArgs e)
{
treeView1.SelectedNode = treeView1.GetNodeAt(treeView1.PointToClient(Cursor.Position));
}
[VB.NET]
Private Sub treeView1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
treeView1.SelectedNode = treeView1.GetNodeAt(treeView1.PointToClient(Cursor.Position))
End Sub

Copyright 2007, Megasolutions Ltd
     
Copyright 2007, Megasolutions Ltd