Export (0) Print
Expand All

TreeView.TopNode Property

Gets or sets the first fully-visible tree node in the tree view control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public TreeNode TopNode { get; set; }

Property Value

Type: System.Windows.Forms.TreeNode
A TreeNode that represents the first fully-visible tree node in the tree view control.

Initially, the TopNode returns the first root tree node, which is located at the top of the TreeView. However, if the user has scrolled the contents, another tree node might be at the top.

NoteNote

Setting this property has no effect when the Scrollable property value is false.

The following code example demonstrates how to use the BeforeLabelEdit AfterSelect and TopNode members. To run this example, paste the following code in a form that contains a TreeView control that is named TreeView1. Call the InitializeTreeView method in the form's constructor or Load method.

	private void InitializeTreeView()
	{

		// Construct the TreeView object. 
		this.TreeView1 = new System.Windows.Forms.TreeView();

		// Set dock, location, size name, and tab order 
		// values for the TreeView object.
		TreeView1.Dock = System.Windows.Forms.DockStyle.Left;
		TreeView1.Location = new System.Drawing.Point(0, 0);
		TreeView1.Name = "TreeView1";
		TreeView1.Size = new System.Drawing.Size(152, 266);
		TreeView1.TabIndex = 1;
		
		// Associate the event-handling methods with the 
		// BeforeLabeEdit and the AfterSelect events.
		TreeView1.BeforeLabelEdit += 
			new NodeLabelEditEventHandler(TreeView1_BeforeLabelEdit);
		TreeView1.AfterSelect += 
			new TreeViewEventHandler(TreeView1_AfterSelect);

		// Set the LabelEdit property to true to allow the  
		// user to edit the TreeNode text. 
		this.TreeView1.LabelEdit = true;

		// Declare and create objects needed to populate  
		// the TreeView. 
		string[] files = new string[]{"bigPresentation.ppt", 
			"myFinances.xls", "myResume.doc"};; 
		string filePath = "c:\\myFiles";
		System.Collections.ArrayList nodes = 
			new System.Collections.ArrayList();

		// Create a node for each file, setting the Text property to the  
		// file's name and the Tag property to file's fully-qualified name. 
		foreach ( string file in files )
		{
			TreeNode node = new TreeNode(file);
			node.Tag = filePath+"\\"+file;
			nodes.Add(node);
		}

		TreeNode[] treeNodes = new TreeNode[nodes.Count];
		nodes.CopyTo(treeNodes);

		// Create a new node named topNode and add the ArrayList of  
		// nodes to topNode.
		TreeNode topNode = new TreeNode("myFiles",  treeNodes);
		topNode.Tag = filePath;

		// Add topNode to the TreeView.
		TreeView1.Nodes.Add(topNode);

		// Add the TreeView to the form. 
		this.Controls.Add(TreeView1);
	}

	private void TreeView1_BeforeLabelEdit(object sender, 
		NodeLabelEditEventArgs e)
	{

		// Determine whether the user has selected the top node. If so, 
		// change the CancelEdit property to true to cancel the edit.   
		if (e.Node == TreeView1.TopNode)

		{
			e.CancelEdit = true;
			MessageBox.Show("You are not allowed to edit the top node");
		}
		
	}


...


	// Handle the After_Select event. 
	private void TreeView1_AfterSelect(System.Object sender, 
		System.Windows.Forms.TreeViewEventArgs e)
	{

		// Vary the response depending on which TreeViewAction 
		// triggered the event.  
		switch((e.Action))
		{
			case TreeViewAction.ByKeyboard:
				MessageBox.Show("You like the keyboard!");
				break;
			case TreeViewAction.ByMouse:
				MessageBox.Show("You like the mouse!");
				break;
		}
	}
		The following method shows the use of the LabelEdit property.
		Add this method to a form containing a ListBox control named ListBox1.

		Call the method in the constructor or Load method of the form.


...


		The following class inherits from the Button class and overrides
		the OnMouseHover and OnMouseMove methods to give the button
		some special behavior. To use this example create a new form and copy paste this class in the same file, after the form class.  Add a button of
		type FunButton, defined in the code example, to the form. 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft