Export (0) Print
Expand All

TreeNode.FromHandle Method

Returns the tree node with the specified handle and assigned to the specified tree view control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public static TreeNode FromHandle (
	TreeView tree,
	IntPtr handle
)
public static TreeNode FromHandle (
	TreeView tree, 
	IntPtr handle
)
public static function FromHandle (
	tree : TreeView, 
	handle : IntPtr
) : TreeNode

Parameters

tree

The TreeView that contains the tree node.

handle

The handle of the tree node.

Return Value

A TreeNode that represents the tree node assigned to the specified TreeView control with the specified handle.

When a node is added to the TreeView.Nodes collection, the TreeView control sends an NM_CUSTOMDRAW notification before the node handle is available. If you override the WndProc method of the TreeView to provide custom drawing in response to this notification, you should always check the return value of this method for a null reference (Nothing in Visual Basic) before attempting to access the node.

The following code example gets the TreeNode that was collapsed and creates a copy of it using its Handle property. The original TreeNode is removed from the TreeNodeCollection and the copy is added to the collection. This example requires that you have a Form with a TreeView control on it. The TreeView control should have two or more root nodes, each having at least one child node.

private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
   // Create a copy of the e.Node from its Handle.
   TreeNode tn = TreeNode.FromHandle(e.Node.TreeView, e.Node.Handle);
   tn.Text += "Copy";
   // Remove the e.Node so it can be replaced with tn.
   e.Node.Remove();
   // Add tn to the TreeNodeCollection.
   treeView1.Nodes.Add(tn);
}

private void treeView1_AfterCollapse(Object sender, TreeViewEventArgs e)
{
    // Create a copy of the e.Node from its Handle.
    TreeNode tn = TreeNode.FromHandle(e.get_Node().get_TreeView(), 
        e.get_Node().get_Handle());
    tn.set_Text(tn.get_Text() + "Copy");
    // Remove the e.Node so it can be replaced with tn.
    e.get_Node().Remove();
    // Add tn to the TreeNodeCollection.
    treeView1.get_Nodes().Add(tn);
} //treeView1_AfterCollapse

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft