This example code shows two important treeview concepts:
To use this code, create a new Windows Form object and add a treeview control. Add the following code to the formís class. Then hook up the MouseDown event to the appropriate code (VB.NET or C#).
Private Sub TreeView1_MouseDown( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Dim NodeClicked As TreeNode
' Get the node clicked on
NodeClicked = Me.TreeView1.GetNodeAt(e.X, e.Y)
' Was a node clicked on
If Not NodeClicked Is Nothing Then
Console.WriteLine("Node is: " & NodeClicked.Text)
Console.WriteLine("No node clicked.")
' By default, right-clicking on a node does not set
' focus to that node. This is especially important when
' you want to use a ContextMenu control and associate
' it with a TreeView. This code shows how to do it.
If e.Button = MouseButtons.Right Then
Me.TreeView1.SelectedNode = NodeClicked
private void TreeViewMouseDown (object sender, MouseEventArgs e)
// Get the node clicked on
nodeClicked = this.TreeView1.GetNodeAt(e.X, e.Y);
// Was the node clicked on?
if (! (nodeClicked == null))
Console.WriteLine ("Node is: " + nodeClicked.Text);
Console.WriteLine("No node clicked.");
// By default, right-clicking on a node does not set focus to
// that node. This is especially important when you want to
// use a ContextMenu control and associate it with a TreeView.
// This code shows how to do it.
if (e.Button == MouseButtons.Right)
this.TreeView1.SelectedNode = nodeClicked;
Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.