C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト

Re[2]: DataGridViewCellのTextAlign


(過去ログ 131 を表示中)

[トピック内 3 記事 (1 - 3 表示)]  << 0 >>

■77259 / inTopicNo.1)  DataGridViewCellのTextAlign
  
□投稿者/ 富内 (1回)-(2015/10/01(Thu) 19:41:23)

分類:[VB.NET/VB2005 以降] 

2015/10/01(Thu) 19:57:15 編集(投稿者)
2015/10/01(Thu) 19:57:02 編集(投稿者)

<pre><pre>お世話になります。
富内と申します。
DataGridViewCellのカスタムセルで、TextAlignをセンタリングしているのですが、フォーカスを失った際に、左寄せに戻ってしまう点で悩んでいます。
開発環境は、Windows8.1(64bit)のVisual Studio Express 2015 for Windows Desktopを使用しております。
ソースの貼り付けで申し訳ありませんが、アドバイスを頂けたら幸いです。

'********************************************
' Form1
'********************************************
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim col As New SampleColumn()
        col.DataPropertyName = "Column1"
        With DataGridView1
            .EditMode = DataGridViewEditMode.EditOnEnter
            .Columns.Add(col)
            .Rows.Add()
            .Rows.Add()
        End With
    End Sub
End Class

'********************************************
' SampleColumn
'********************************************
Public Class SampleColumn
    Inherits DataGridViewColumn

    Public Sub New()
        MyBase.New(New SampleCell)
    End Sub

    Public Overrides Property CellTemplate As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(value As DataGridViewCell)
            MyBase.CellTemplate = DirectCast(value, SampleCell)
        End Set
    End Property

    Public Overrides Function Clone() As Object
        Dim cloneObj As SampleColumn = MyBase.Clone
        Return cloneObj
    End Function

End Class

'********************************************
' SampleCell
'********************************************
Public Class SampleCell
    Inherits DataGridViewTextBoxCell

    Public Overrides Sub InitializeEditingControl(rowIndex As Integer,
                                                  initialFormattedValue As Object,
                                                  dataGridViewCellStyle As DataGridViewCellStyle)

        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        Dim sampleBox As SampleBox = TryCast(MyBase.DataGridView.EditingControl, SampleEditingControl)

        If sampleBox IsNot Nothing Then
            Dim text As String = TryCast(initialFormattedValue, String)
            sampleBox.Text = If(text IsNot Nothing, text, "")
            sampleBox.TextAlign = HorizontalAlignment.Center
        End If

    End Sub

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

End Class

'********************************************
' SampleEditingControl
'********************************************
Public Class SampleEditingControl
    Inherits SampleBox
    Implements IDataGridViewEditingControl

    Private dgv As DataGridView
    Private isValueChanged As Boolean
    Private rowNum As Integer

    Public Property EditingControlDataGridView As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
        Get
            Return dgv
        End Get
        Set(value As DataGridView)
            dgv = value
        End Set
    End Property

    Public Property EditingControlFormattedValue As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return Me.GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting)
        End Get
        Set(value As Object)
            MyBase.Text = DirectCast(value, String)
        End Set
    End Property

    Public Property EditingControlRowIndex As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
        Get
            Return rowNum
        End Get
        Set(value As Integer)
            rowNum = value
        End Set
    End Property

    Public Property EditingControlValueChanged As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
        Get
            Return isValueChanged
        End Get
        Set(value As Boolean)
            isValueChanged = value
        End Set
    End Property

    Public ReadOnly Property EditingPanelCursor As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
        Get
            Return MyBase.Cursor
        End Get
    End Property

    Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        Get
            Return False
        End Get
    End Property

    Public Sub ApplyCellStyleToEditingControl(dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
        Me.TextAlign = HorizontalAlignment.Center
    End Sub

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

    End Sub

    Public Function EditingControlWantsInputKey(keyData As Keys, dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
        Return False
    End Function

    Public Function GetEditingControlFormattedValue(context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
        Return MyBase.Text
    End Function

    Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
        MyBase.OnTextChanged(e)
        Me.isValueChanged = True
        Me.dgv.NotifyCurrentCellDirty(True)
    End Sub

End Class

'********************************************
' SampleBox
'********************************************
Public Class SampleBox
    Inherits TextBox

    Public Sub New()
        Me.TextAlign = HorizontalAlignment.Center
    End Sub

    Private Sub SampleBox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        Me.TextAlign = HorizontalAlignment.Center
    End Sub

End Class

よろしくお願いします。</pre></pre>

引用返信 編集キー/
■77260 / inTopicNo.2)  Re[1]: DataGridViewCellのTextAlign
□投稿者/ Hongliang (348回)-(2015/10/01(Thu) 20:10:16)
非編集時のテキストの位置は、DataGridViewCellStyle::Alignmentですね。
DataGridViewTextBoxColumn::DefaultCellStyleとかで設定すればいいんじゃないでしょうか。
引用返信 編集キー/
■77262 / inTopicNo.3)  Re[2]: DataGridViewCellのTextAlign
□投稿者/ 富内 (2回)-(2015/10/01(Thu) 20:39:59)
No77260 (Hongliang さん) に返信
> 非編集時のテキストの位置は、DataGridViewCellStyle::Alignmentですね。
> DataGridViewTextBoxColumn::DefaultCellStyleとかで設定すればいいんじゃないでしょうか。

Hongliang さん、アドバイスありがとうございました。
以下のようにして解決することができました。

'********************************************
' SampleColumn
'********************************************
Public Class SampleColumn
    Inherits DataGridViewColumn

    Public Sub New()
        MyBase.New(New SampleCell)
        Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter '←ここ
    End Sub

    Public Overrides Property CellTemplate As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(value As DataGridViewCell)
            If TypeOf value IsNot SampleCell Then
                MyBase.CellTemplate = value
            End If
        End Set
    End Property

    Public Overrides Function Clone() As Object
        Dim cloneObj As SampleColumn = MyBase.Clone
        Return cloneObj
    End Function

End Class

まだまだ勉強不足でした。
むずかしい…

ありがとうございました。

解決済み
引用返信 編集キー/


トピック内ページ移動 / << 0 >>

このトピックに書きこむ

過去ログには書き込み不可

管理者用

- Child Tree -