the LabelEdit property in TreeView class

Today I want to add the RENAME feature to maintain the Test Case Tree in StepRecorder, which exactly like the operation in Windows Explorer. After user right click a node, the selected node turns to Edit Mode, and user could type the new name in the edit box. System will ask user to reinput the new name if some invalid characters are entered such as "!"/","/"@"... and the selected node will remain in its Edit Mode for user's re-input.

To carry out this function, TreeView.LabelEdit property, TreeNode.BeginEdit() and TreeNode.EndEdit(bool) will be used as well as the TreeView.AfterLabelEdit event.

Here is a demo code from MSDN and I think it greatly show how these property/method/event work together.

/* 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;
}
}