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

Comments

  • Anonymous
    January 25, 2006
    What version of the .NET Framework is this for? For 2.0, if you just always set the LabelEdit property, then select and click on a node, it automatically does the begin edit for you. You don't need the code that you have to begin and end editing. You just need the AfterLabelEdit handler to implement the functionality you have. And, you only need to set the event args cancel property not actually call EndEdit.
  • Anonymous
    January 25, 2006
    This is truely for .NET Framework 2.0. You are right that after LabelEdit is set the whole TreeView will be in Edit mode and user could edit each node with mouse-click.

    In this sample codes, use BeginEdit() method could point out which node will be in Edit mode and user could input new value to the node name without any mouse-click. For example in a scenario that user is forbidden to edit node name untill "Rename" context menu is selected.