Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

NodeLabelEditEventArgs Class

Provides data for the BeforeLabelEdit and AfterLabelEdit events.

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

public class NodeLabelEditEventArgs : EventArgs
public class NodeLabelEditEventArgs extends EventArgs
public class NodeLabelEditEventArgs extends EventArgs
Not applicable.

The AfterLabelEdit event occurs when the user finishes editing the text for a tree node. The BeforeLabelEdit event occurs when the user begins editing the text for a tree node. A NodeLabelEditEventArgs object specifies the new text to associate with the tree node, the tree node that contains the label to edit, and whether the edit operation has been canceled.

For more information about handling events, see Consuming Events.

The following example allows the user to edit nonroot tree nodes by using a ContextMenu. When the user right clicks the mouse, the TreeNode at that position is determined and stored in a variable named mySelectedNode. If a nonroot tree node was selected, it is put into an editable state, allowing the user to edit the node label. After the user stops editing the tree node label, the new label text is evaluated and saved. For this example, several characters are considered not valid in the label text. If one of the invalid characters is in the label string, or the string is empty, the user is notified of the error and the label is returned to its previous text.

/* Get the tree node under the mouse pointer and 
   save it in the mySelectedNode variable. */
private void treeView1_MouseDown(object sender, 
  System.Windows.Forms.MouseEventArgs e)
{
   mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
   if (mySelectedNode != null && mySelectedNode.Parent != null)
   {
      treeView1.SelectedNode = mySelectedNode;
      treeView1.LabelEdit = true;
      if(!mySelectedNode.IsEditing)
      {
         mySelectedNode.BeginEdit();
      }
   }
   else
   {
      MessageBox.Show("No tree node selected or selected node is a root node.\n" + 
         "Editing of root nodes is not allowed.", "Invalid selection");
   }
}

private void treeView1_AfterLabelEdit(object sender, 
         System.Windows.Forms.NodeLabelEditEventArgs e)
{
   if (e.Label != null)
   {
     if(e.Label.Length > 0)
     {
        if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
        {
           // Stop editing without canceling the label change.
           e.Node.EndEdit(false);
        }
        else
        {
           /* Cancel the label edit action, inform the user, and 
              place the node in edit mode again. */
           e.CancelEdit = true;
           MessageBox.Show("Invalid tree node label.\n" + 
              "The invalid characters are: '@','.', ',', '!'", 
              "Node Label Edit");
           e.Node.BeginEdit();
        }
     }
     else
     {
        /* Cancel the label edit action, inform the user, and 
           place the node in edit mode again. */
        e.CancelEdit = true;
        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", 
           "Node Label Edit");
        e.Node.BeginEdit();
     }
     this.treeView1.LabelEdit = false;
   }
}

/* Get the tree node under the mouse pointer and 
   save it in the mySelectedNode variable. 
 */
private void treeView1_MouseDown(Object sender, 
                                 System.Windows.Forms.MouseEventArgs e)
{
    mySelectedNode = treeView1.GetNodeAt(e.get_X(), e.get_Y());
} //treeView1_MouseDown

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    if (mySelectedNode != null && mySelectedNode.get_Parent() != null) {
        treeView1.set_SelectedNode(mySelectedNode);
        treeView1.set_LabelEdit(true);
        if (!(mySelectedNode.get_IsEditing())) {
            mySelectedNode.BeginEdit();
        }
    }
    else {
        MessageBox.Show("No tree node selected or selected node"
            + "is a root node.\n" 
            + "Editing of root nodes is not allowed.", "Invalid selection");
    }
} //menuItem1_Click

private void treeView1_AfterLabelEdit(Object sender, 
                            System.Windows.Forms.NodeLabelEditEventArgs e)
{
    if (e.get_Label()!= null) {
        if (e.get_Label().length() > 0) {
            if (e.get_Label().IndexOfAny((new char[]{ '@', '.', ',', '!' }))
                == -1) {
                // Stop editing without canceling the label change.
                e.get_Node().EndEdit(false);
            }
            else {
                /* Cancel the label edit action, inform the user, and 
                   place the node in edit mode again. 
                 */
                e.set_CancelEdit(true);
                MessageBox.Show("Invalid tree node label.\n" 
                    + "The invalid characters are: "
                    + "'@','.', ',', '!'", "Node Label Edit");
                e.get_Node().BeginEdit();
            }
        }
        else {
            /* Cancel the label edit action, inform the user, and 
               place the node in edit mode again.
             */
            e.set_CancelEdit(true);
            MessageBox.Show("Invalid tree node label.\n"
                + "The label cannot be blank", "Node Label Edit");
            e.get_Node().BeginEdit();
        }
        this.treeView1.set_LabelEdit(false);
    }
} //treeView1_AfterLabelEdit

System.Object
   System.EventArgs
    System.Windows.Forms.NodeLabelEditEventArgs
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.