TreeView for WinForms
カスタムエディター

デフォルトでは、TreeView のノードの編集にはテキストボックスエディターが使用されます。ただし、必要な場合はいつでも、デフォルトのエディターをカスタムエディターに置き換えることができます。C1TreeColumn の Editor プロパティを使用して、ノードのカスタムエディターを列ごとに指定できます。さらに、C1TreeViewEditorType 列挙から EditorType プロパティを設定して、ノードエディターのタイプを設定することができます。

次の図に、デフォルトのテキストボックスエディタとカスタムテキストボックスエディターの両方を示します。

次のコードスニペットは、エディタのサンプルデータを提供するクラス EditorsData を作成します。

Imports System.ComponentModel

Namespace SamplesData
    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 {
                    Key.TextBoxValue = i
                })
                For j As Integer = 0 To 4
                    data(i).Collection.Add(New EditorsData() With {
                        Key.TextBoxValue = i + j
                    })
                Next
            Next

            Return data
        End Function
    End Class
using System.ComponentModel;

namespace SamplesData
{
    public class EditorsData
    {
        public object ComboBoxValue
        {
            get; set;
        }

        public object TextBoxValue
        {
            get; set;
        }

        public bool CheckBoxValue
        {
            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() { CheckBoxValue = true, TextBoxValue = i, 
                ComboBoxValue = i });
                for (int j = 0; j < 5; j++)
                {
                    data[i].Collection.Add(new EditorsData() { CheckBoxValue = false, 
                    TextBoxValue = i + j, ComboBoxValue = i + j });
                }
            }

            return data;
        }
    }
}

次のコードスニペットは、デフォルトのテキストボックスエディタに基づいて、カスタムテキストボックスエディターを作成するクラス CustomTextBox を作成します。

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;
            BackColor = Color.Blue;
            WordWrap = 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;
        }
    }
}

次のコードスニペットは、デフォルトのテキストボックスとカスタムテキストボックスをそれぞれ 1 列目と 2 列目のエディターとして設定します。

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 = "テキストボックス"
column1.Width = 60
column1.DisplayFieldName = "TextBoxValue\TextBoxValue"
column1.Editor = textBox1
column1.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text

column2.Name = "cnlCustomTextBox"
column2.HeaderText = "カスタムテキストボックス"
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 = "テキストボックス";
column1.Width = 60;
column1.DisplayFieldName = "TextBoxValue\\TextBoxValue";
column1.Editor = textBox1;
column1.EditorType = C1.Win.TreeView.C1TreeViewEditorType.Text;

column2.Name = "cnlCustomTextBox";
column2.HeaderText = "カスタムテキストボックス";
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();