分類:[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>