TreeView for WinForms | ComponentOne
Node Operations / Edit Nodes
In This Topic
    Edit Nodes
    In This Topic

    TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView class to true. The default value of the property is false.

    You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node itself. In addition, you can edit a node programmatically by calling the BeginEdit method in code. To use any of these modes for editing nodes, you need to set the EditMode property of the C1TreeView class from the C1TreeViewEditMode enum.

    Note: If you want to determine whether the contents of a specific node can be changed, you can use the IsReadOnly method of C1TreeNode.

    The following image shows a node being edited.

    Node editing

    The given code snippet sets the AllowEditing property to allow node editing and specifies the mode of editing.

    ' set the AllowEditing property
    C1TreeView1.AllowEditing = True
    
    ' set the EditMode property
    C1TreeView1.EditMode = C1.Win.TreeView.C1TreeViewEditMode.EditOnEnter
    
    // set the AllowEditing property
    c1TreeView1.AllowEditing = true;
    
    // set the EditMode property
    c1TreeView1.EditMode = C1.Win.TreeView.C1TreeViewEditMode.EditOnEnter;
    

    Custom Editors

    TreeView, by default, uses a textbox editor for editing nodes. You can, however, replace the default editor with a custom editor, whenever needed. You can specify a custom editor for nodes for each column by using the Editor property of C1TreeColumn. In addition, you can set the type of the node editor by setting the EditorType property from the C1TreeViewEditorType enum.

    The following image displays both the default textbox and the custom textbox editor.

    custom textbox editor

    The following code snippet creates a class EditorsData that provides sample data for the editors.

    Imports System.ComponentModel
    Public Class EditorsData
        Public Property TextBoxValue() As Object
            Get
                Return m_TextBoxValue
            End Get
            Set
                m_TextBoxValue = Value
            End Set
        End Property
        Private m_TextBoxValue As Object
    
        Public Property Collection() As BindingList(Of EditorsData)
            Get
                Return m_Collection
            End Get
            Set
                m_Collection = Value
            End Set
        End Property
        Private m_Collection As BindingList(Of EditorsData)
    
        Public Sub New()
            Collection = New BindingList(Of EditorsData)()
        End Sub
    
        Public Shared Function GetData() As BindingList(Of EditorsData)
            Dim data = New BindingList(Of EditorsData)()
            For i As Integer = 0 To 4
                data.Add(New EditorsData() With {
                    .m_TextBoxValue = i
                })
                For j As Integer = 0 To 4
                    data(i).Collection.Add(New EditorsData() With {
                    .m_TextBoxValue = i + j
                    })
                Next
            Next
    
            Return data
        End Function
    End Class
    
    using System.ComponentModel;
    
    namespace SamplesData
    {
        public class EditorsData
        {
            public object TextBoxValue
            {
                get; set;
            }
    
            public BindingList<EditorsData> Collection
            { get; set; }
    
            public EditorsData()
            {
                Collection = new BindingList<EditorsData>();
            }
    
            public static BindingList<EditorsData> GetData()
            {
                var data = new BindingList<EditorsData>();
                for (int i = 0; i < 5; i++)
                {
                    data.Add(new EditorsData() {TextBoxValue = i,});
                    for (int j = 0; j < 5; j++)
                    {
                        data[i].Collection.Add(new EditorsData() {TextBoxValue = i + j});
                    }
                }
    
                return data;
            }
        }
    }
    

    The following code snippet creates a class CustomTextBox to create a custom textbox editor on the basis of the default textbox editor.

    Imports C1.Win.TreeView
    
    Public Class CustomTextBox
        Inherits TextBox
        Implements IC1TreeEditor
        Public Function C1EditorGetValue() As Object
            Return Text
        End Function
    
        Public Sub C1EditorInitialize(value As Object, attrs As IDictionary)
            BorderStyle = BorderStyle.FixedSingle
            AutoSize = True
            TabStop = True
    
            If attrs.Contains("AcceptReturn") Then
                AcceptsReturn = CBool(attrs("AcceptReturn"))
            End If
            If attrs.Contains("AcceptTab") Then
                AcceptsTab = CBool(attrs("AcceptTab"))
            End If
            If attrs.Contains("BackColor") Then
                BackColor = DirectCast(attrs("BackColor"), Color)
            End If
            If attrs.Contains("Font") Then
                Font = DirectCast(attrs("Font"), Font)
            End If
            If attrs.Contains("ForeColor") Then
                ForeColor = DirectCast(attrs("ForeColor"), Color)
            End If
            If attrs.Contains("DisabledForeColor") Then
                ForeColor = DirectCast(attrs("DisabledForeColor"), Color)
            End If
            If attrs.Contains("MaxLength") Then
                MaxLength = CInt(attrs("MaxLength"))
            End If
            If attrs.Contains("ReadOnly") Then
                [ReadOnly] = CBool(attrs("ReadOnly"))
            End If
            If attrs.Contains("WordWrap") Then
                WordWrap = CBool(attrs("WordWrap"))
            End If
    
            Text = value.ToString()
        End Sub
    
        Public Function C1EditorKeyDownFinishEdit(e As KeyEventArgs) As Boolean
            If e.KeyData = Keys.Enter Then
                Return True
            End If
            Return False
        End Function
    
        Public Sub C1EditorUpdateBounds(rc As Rectangle)
            Bounds = rc
        End Sub
    
        Public Function C1EditorValueIsValid() As Boolean
            Return True
        End Function
    
        Private Function IC1TreeEditor_C1EditorValueIsValid()
            As Boolean Implements IC1TreeEditor.C1EditorValueIsValid
            Throw New NotImplementedException()
        End Function
    
        Private Sub IC1TreeEditor_C1EditorUpdateBounds(rc As Rectangle)
            Implements IC1TreeEditor.C1EditorUpdateBounds
            Throw New NotImplementedException()
        End Sub
    
        Private Function IC1TreeEditor_C1EditorKeyDownFinishEdit(e As KeyEventArgs)
            As Boolean Implements IC1TreeEditor.C1EditorKeyDownFinishEdit
            Throw New NotImplementedException()
        End Function
    
        Private Sub IC1TreeEditor_C1EditorInitialize(value As Object, attrs As IDictionary)
            Implements IC1TreeEditor.C1EditorInitialize
            Throw New NotImplementedException()
        End Sub
    
        Private Function IC1TreeEditor_C1EditorGetValue()
            As Object Implements IC1TreeEditor.C1EditorGetValue
            Throw New NotImplementedException()
        End Function
    End Class
    
    using C1.Win.TreeView;
    using System.Collections;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace CustomEditors
    {
        public class CustomTextBox : TextBox, IC1TreeEditor
        {
            public object C1EditorGetValue()
            {
                return Text;
            }
    
            public void C1EditorInitialize(object value, IDictionary attrs)
            {
                BorderStyle = BorderStyle.FixedSingle;
                AutoSize = true;
                TabStop = true;
    
                if (attrs.Contains("AcceptReturn"))
                    AcceptsReturn = (bool)attrs["AcceptReturn"];
                if (attrs.Contains("AcceptTab"))
                    AcceptsTab = (bool)attrs["AcceptTab"];
                if (attrs.Contains("BackColor"))
                    BackColor = (Color)attrs["BackColor"];
                if (attrs.Contains("Font"))
                    Font = (Font)attrs["Font"];
                if (attrs.Contains("ForeColor"))
                    ForeColor = (Color)attrs["ForeColor"];
                if (attrs.Contains("DisabledForeColor"))
                    ForeColor = (Color)attrs["DisabledForeColor"];
                if (attrs.Contains("MaxLength"))
                    MaxLength = (int)attrs["MaxLength"];
                if (attrs.Contains("ReadOnly"))
                    ReadOnly = (bool)attrs["ReadOnly"];
                if (attrs.Contains("WordWrap"))
                    WordWrap = (bool)attrs["WordWrap"];
    
                Text = value.ToString();
            }
    
            public bool C1EditorKeyDownFinishEdit(KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                    return true;
                return false;
            }
    
            public void C1EditorUpdateBounds(Rectangle rc)
            {
                Bounds = rc;
            }
    
            public bool C1EditorValueIsValid()
            {
                return true;
            }
        }
    }
    

    The following code snippet sets the default textbox and the custom textbox as editors in the first and the second columns, respectively.

    Dim textBox1 As System.Windows.Forms.TextBox = New TextBox()
    Dim textBox2 As New CustomTextBox()
    C1TreeView1.AllowEditing = True
    
    C1TreeView1.Columns.Clear()
    
    Dim column1 As New C1.Win.TreeView.C1TreeColumn()
    Dim column2 As New C1.Win.TreeView.C1TreeColumn()
    Dim column3 As New C1.Win.TreeView.C1TreeColumn()
    Dim column4 As New C1.Win.TreeView.C1TreeColumn()
    
    C1TreeView1.Columns.Add(column1)
    C1TreeView1.Columns.Add(column2)
    
    column1.Name = "clnTextBox"
    column1.HeaderText = "TextBox"
    column1.Width = 60
    column1.DisplayFieldName = "TextBoxValue\TextBoxValue"
    column1.Editor = textBox1
    column1.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text
    
    column2.Name = "cnlCustomTextBox"
    column2.HeaderText = "Custom TextBox"
    column2.Width = 90
    column2.DisplayFieldName = "TextBoxValue\TextBoxValue"
    column2.Editor = New CustomTextBox()
    column2.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text
    
    C1TreeView1.DataMember = "Collection\Collection"
    C1TreeView1.DataSource = EditorsData.GetData()
    
    System.Windows.Forms.TextBox textBox1 = new TextBox();
    CustomTextBox textBox2 = new CustomTextBox();
    c1TreeView1.AllowEditing = true;
    
    C1.Win.TreeView.C1TreeColumn column1 = new C1.Win.TreeView.C1TreeColumn();
    C1.Win.TreeView.C1TreeColumn column2 = new C1.Win.TreeView.C1TreeColumn();
    C1.Win.TreeView.C1TreeColumn column3 = new C1.Win.TreeView.C1TreeColumn();
    C1.Win.TreeView.C1TreeColumn column4 = new C1.Win.TreeView.C1TreeColumn();
    
    c1TreeView1.Columns.Add(column1);
    c1TreeView1.Columns.Add(column2);
    
    column1.Name = "clnTextBox";
    column1.HeaderText = "TextBox";
    column1.Width = 60;
    column1.DisplayFieldName = "TextBoxValue\\TextBoxValue";
    column1.Editor = textBox1;
    column1.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text;
    
    column2.Name = "cnlCustomTextBox";
    column2.HeaderText = "Custom TextBox";
    column2.Width = 90;
    column2.DisplayFieldName = "TextBoxValue\\TextBoxValue";
    column2.Editor = new CustomTextBox();
    column2.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text;
    
    c1TreeView1.DataMember = "Collection\\Collection";
    c1TreeView1.DataSource = EditorsData.GetData();