TreeView.BeforeLabelEdit Event
Occurs before the tree node label text is edited.
[Visual Basic] Public Event BeforeLabelEdit As NodeLabelEditEventHandler [C#] public event NodeLabelEditEventHandler BeforeLabelEdit; [C++] public: __event NodeLabelEditEventHandler* BeforeLabelEdit;
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.
Event Data
The event handler receives an argument of type NodeLabelEditEventArgs containing data related to this event. The following NodeLabelEditEventArgs properties provide information specific to this event.
| Property | Description |
|---|---|
| CancelEdit | Gets or sets a value indicating whether the edit has been canceled. |
| Label | Gets the new text to associate with the tree node. |
| Node | Gets the tree node containing the text to edit. |
Remarks
For more information about handling events, see Consuming Events.
Example
[Visual Basic, C#] The following code example demonstrates how to use the event and the property. To run the example paste the following code in a form containing a TreeView control called TreeView1. Call the InitializeTreeView method in the form's constructor or Load method.
[Visual Basic] Private Sub InitializeTreeView() ' Construct the TreeView object. Me.TreeView1 = New System.Windows.Forms.TreeView ' Set dock, location, size name, and tab order ' values for the TreeView object. With TreeView1 .Dock = System.Windows.Forms.DockStyle.Left .Location = New System.Drawing.Point(0, 0) .Name = "TreeView1" .Size = New System.Drawing.Size(152, 266) .TabIndex = 1 End With ' Set the LabelEdit property to true to allow the ' user to edit the TreeNode text. Me.TreeView1.LabelEdit = True ' Declare and create objects needed to populate ' the TreeView. Dim file, files(), filePath As String files = New String() {"bigPresentation.ppt", "myFinances.xls", _ "myResume.doc"} filePath = "c:\myFiles" Dim nodes As 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. For Each file In files Dim node As New TreeNode(file) node.Tag = filePath & "\" & file nodes.Add(node) Next ' Create a new node named topNode and add the ArrayList of ' nodes to topNode. Dim topNode As New TreeNode("myFiles", _ nodes.ToArray(GetType(TreeNode))) topNode.Tag = filePath ' Add topNode to the TreeView. TreeView1.Nodes.Add(topNode) ' Add the TreeView to the form. Me.Controls.Add(TreeView1) End Sub Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, _ ByVal e As NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit ' Determine whether the user has selected the top node. If so, ' change the LabelEdit property to false so the user cannot ' edit this label. If (e.Node Is TreeView1.TopNode) Then TreeView1.LabelEdit = False MessageBox.Show("You are not allowed to edit the top node") End If ' Set the LabelEdit property to true again. TreeView1.LabelEdit = True End Sub [C#] 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 LabelEdit property to false so the user cannot // edit this label. if (e.Node == TreeView1.TopNode) { TreeView1.LabelEdit = false; MessageBox.Show("You are not allowed to edit the top node"); } // Set the LabelEdit property to true again. TreeView1.LabelEdit = true; }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
TreeView Class | TreeView Members | System.Windows.Forms Namespace | OnBeforeLabelEdit | AfterLabelEdit | OnAfterLabelEdit