none
DataGridViewTextBoxColumnの追加プロパティの保存方法について RRS feed

  • 質問

  • VB.NET2005で、DataGridViewTextBoxColumnを継承したカスタムコントロールを作成していて困った事があるので、投稿させて頂きます。

    最初に通常のTextBoxコントロールを継承して、プロパティを拡張したコントロールを作成しました。(以下、MyTextBoxとし、拡張プロパティはKeyLimitとします。)

    MyTextBoxを継承したEditingControlを作成

    DataGridViewTextBoxCellを継承したMyTextBoxCellを作成

    DataGridViewTextBoxColumnを継承したMyTextBoxColumnを作成しました。

    MyTextBoxColumnにKeyLimitというプロパティを用意し、MyTextBoxのKeyLimitプロパティに渡す様にしているのですが、コードで指定した場合は正しく渡っていて正しく動作しています。(KeyLimitは文字の入力制限をしています。)

    しかし、列の編集でプロパティを変更しても画面のDesignerに保存されず、もう一度列の編集を開くとデフォルトの値に戻ってしまい、困っています。

    CellTemplateプロパティがあやしいと思っているのですが、どうしたら良いのか分かりません。何かヒントがありましたらご教授願います。

    以下のページに同じ話題があり参考にさせて頂いたのですが、うまく行きませんでした。

    http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=34107&forum=7&9

    以下、ソースです。(長くて申し訳ありません。)

    ' MyTextBoxコントロール

    Public Class TextBox

        Inherits System.Windows.Forms.TextBox

        Private shtKeyLimit As Short
        Public Overridable Property KeyLimit() As Short
            Get
                Return shtKeyLimit
            End Get
            Set(ByVal Value As Short)
                shtKeyLimit = Value
            End Set
        End Property

    ・・・以下省略
    End Class

    'MyTextBoxEditingControlコントロール

    Public Class MyTextBoxEditingControl

        Inherits MyTextBox
        Implements IDataGridViewEditingControl

        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer

        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.Text
            End Get
            Set(ByVal value As Object)
                If TypeOf value Is [String] Then
                    Me.Text = value
                End If
            End Set
        End Property

        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

            Return Me.Text
        End Function

        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

            Me.Font = dataGridViewCellStyle.Font
            Me.ForeColor = dataGridViewCellStyle.ForeColor
            Me.BackColor = dataGridViewCellStyle.BackColor
            Me.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment)

            Dim cell As TextBoxCell = EditingControlDataGridView.CurrentCell
            Dim col As TextBoxColumn = cell.OwningColumn
            Me.KeyLimit = col.KeyLimit
            Me.MaxLength = col.MaxInputLength
        End Sub

        Public Property EditingControlRowIndex() As Integer _
            Implements IDataGridViewEditingControl.EditingControlRowIndex

            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set

        End Property

        Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
            Implements IDataGridViewEditingControl.EditingControlWantsInputKey

            Select Case key And Keys.KeyCode
                Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
                    Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp

                    Return True

                Case Else
                    Return False
            End Select

        End Function

        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
            Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

            If selectAll Then
                Me.SelectAll()
            End If
        End Sub

        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean _
            Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange

            Get
                Return False
            End Get

        End Property

        Public Property EditingControlDataGridView() As DataGridView _
            Implements IDataGridViewEditingControl.EditingControlDataGridView

            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set

        End Property

        Public Property EditingControlValueChanged() As Boolean _
            Implements IDataGridViewEditingControl.EditingControlValueChanged

            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set

        End Property

        Public ReadOnly Property EditingControlCursor() As Cursor _
            Implements IDataGridViewEditingControl.EditingPanelCursor

            Get
                Return MyBase.Cursor
            End Get

        End Property

        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            valueIsChanged = True
            If dataGridViewControl IsNot Nothing Then
                dataGridViewControl.NotifyCurrentCellDirty(True)
            End If
            MyBase.OnTextChanged(e)
        End Sub

        Private Shared Function translateAlignment(ByVal align As DataGridViewContentAlignment) As HorizontalAlignment
            Select Case align
                Case DataGridViewContentAlignment.TopLeft, DataGridViewContentAlignment.MiddleLeft, DataGridViewContentAlignment.BottomLeft
                    Return HorizontalAlignment.Left

                Case DataGridViewContentAlignment.TopCenter, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.BottomCenter
                    Return HorizontalAlignment.Center

                Case DataGridViewContentAlignment.TopRight, DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.BottomRight
                    Return HorizontalAlignment.Right
            End Select

            Return HorizontalAlignment.Left
        End Function
    End Class

    'MyTextBoxCellクラス

    Public Class MyTextBoxCell
        Inherits DataGridViewTextBoxCell

        Private shtKeyLimit As Short
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)

            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

            Dim ctl As TextBoxEditingControl = DirectCast(DataGridView.EditingControl, MyTextBoxEditingControl)
            ctl.Text = DirectCast(Me.Value, String)
            ctl.KeyLimit = enmKeyLimit
            ctl.MaxLength = Me.MaxInputLength

        End Sub

        Public Overrides ReadOnly Property EditType() As Type
            Get
                Return GetType(MyTextBoxEditingControl)
            End Get
        End Property

        Public Overrides ReadOnly Property ValueType() As Type
            Get
                Return GetType(String)
            End Get
        End Property

        Public Property KeyLimit() As Short
            Get
                Return shtKeyLimit
            End Get
            Set(ByVal value As Short)
                shtKeyLimit = value
            End Set
        End Property

        Public Overridable Shadows Function Clone() As Object
            Dim cloneObj As TextBoxColumn = MyBase.Clone
            cloneObj.KeyLimit = shtKeyLimit
            cloneObj.MaxInputLength = Me.MaxInputLength
            Return cloneObj
        End Function

    End Class

    'MyTextBoxColumnクラス

    Public Class MyTextBoxColumn
        Inherits DataGridViewTextBoxColumn

        Public Sub New()
            MyBase.New()
            MyBase.CellTemplate = New MyTextBoxCell()
        End Sub

        Private Shadows Property CellTemplate() As MyTextBoxCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As MyTextBoxCell)
                MyBase.CellTemplate = value
            End Set
        End Property

        Public Property KeyLimit() As Short
            Get
                Return DirectCast(MyBase.CellTemplate, MyTextBoxCell).KeyLimit
            End Get
            Set(ByVal value As Short)
                DirectCast(MyBase.CellTemplate, MyTextBoxCell).KeyLimit = value
            End Set
        End Property

    End Class

    2006年12月8日 7:31

回答

  • お騒がせ致しました。解決致しました。

    以下の部分を変更したら、うまくいきました。

    Cloneメソッドの呼び出しと、単純な間違いをしていました。

    'MyTextBoxCellクラスはCloneメソッドを修正

        Public Overrides Function Clone() As Object
            Dim cloneObj As MyTextBoxCell = MyBase.Clone
            cloneObj.KeyLimit = shtKeyLimit
            cloneObj.MaxInputLength = Me.MaxInputLength
            Return cloneObj
        End Function

    'MyTextBoxColumnクラスにCloneメソッドを追加

        Public Overrides Function Clone() As Object
            Dim cloneObj As MyTextBoxColumn = MyBase.Clone
            cloneObj.KeyLimit = Me.KeyLimit
            cloneObj.MaxInputLength = Me.MaxInputLength
            Return cloneObj
        End Function

    お騒がせ致しました。

    2006年12月11日 6:09