Programmatically editing of tree node in TreeListView .NET control
October 01, 2008
In order to start edit process for node text during runtime, you need to follow these steps:
- Set the LabelEdit property to True
- Call the BeginEdit method for the node which you want to edit
- (Optional) Handle the BeginLabelEdit to perform tasks before the user edits the node text
- (Optional) Handle the AfterLabelEdit when the user finishes modifying the node text
- (Optional) Call the EndEdit method to finish the edit process.
In the following example a message appear whenever user tries to insert some invalid characters in the newly edited label. The edit process is canceled and restarted.
private void treeListView1_AfterLabelEdit(object sender, LidorSystems.IntegralUI.ObjectEditEventArgs e)
{
if (e.Object is LidorSystems.IntegralUI.Lists.TreeListViewNode)
{
LidorSystems.IntegralUI.Lists.TreeListViewNode node = (LidorSystems.IntegralUI.Lists.TreeListViewNode)e.Object;
if (e.Label != null)
{
if (e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[] { '@', '.', ',', '!' }) >= 0)
{
// Cancel the label edit action, inform the user, and place the node in edit mode again
e.Cancel = true;
MessageBox.Show("Invalid tree node label.\nThe invalid characters are: '@','.', ',', '!'", "Node Label Edit");
node.BeginEdit();
}
}
else
{
// Cancel the label edit action, inform the user, and place the node in edit mode again
e.Cancel = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", "Node Label Edit");
node.BeginEdit();
}
}
}
}
Private Sub treeListView1_AfterLabelEdit(sender As Object, e As LidorSystems.IntegralUI.ObjectEditEventArgs)
If TypeOf e.[Object] Is LidorSystems.IntegralUI.Lists.TreeListViewNode Then
Dim node As LidorSystems.IntegralUI.Lists.TreeListViewNode = DirectCast(e.[Object], LidorSystems.IntegralUI.Lists.TreeListViewNode)
If e.Label IsNot Nothing Then
If e.Label.Length > 0 Then
If e.Label.IndexOfAny(New Char() {"@"c, "."c, ","c, "!"c}) >= 0 Then
' Cancel the label edit action, inform the user, and place the node in edit mode again
e.Cancel = True
MessageBox.Show("Invalid tree node label." & vbLf & "The invalid characters are: '@','.', ',', '!'", "Node Label Edit")
node.BeginEdit()
End If
Else
' Cancel the label edit action, inform the user, and place the node in edit mode again
e.Cancel = True
MessageBox.Show("Invalid tree node label." & vbLf & "The label cannot be blank", "Node Label Edit")
node.BeginEdit()
End If
End If
End If
End Sub
