Public Class tbCellRTL
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value/RTL of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
DataGridView.EditingControl.RightToLeft = Me._RightToLeft
DataGridView.EditingControl.Text = Me.Value
End Sub
Private _RightToLeft As System.Windows.Forms.RightToLeft = Windows.Forms.RightToLeft.Yes
Public Property RightToLeft() As System.Windows.Forms.RightToLeft
Get
Return Me._RightToLeft
End Get
Set(ByVal value As System.Windows.Forms.RightToLeft)
Me._RightToLeft = value
End Set
End Property
Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, _
ByVal clipBounds As System.Drawing.Rectangle, _
ByVal cellBounds As System.Drawing.Rectangle, _
ByVal rowIndex As Integer, _
ByVal cellState As System.Windows.Forms.DataGridViewElementStates, _
ByVal value As Object, ByVal formattedValue As Object, _
ByVal errorText As String, _
ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, _
ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
If Me.IsInEditMode = False Then
'when not editing draw the datagridviewcell (else textbox editing control draws itself)
If Me._RightToLeft = Windows.Forms.RightToLeft.Yes Then
If (cellState And DataGridViewElementStates.Displayed) = DataGridViewElementStates.Displayed Then
cellBounds.Offset(-1, -1)
Dim sz As SizeF = graphics.MeasureString(Me.Value, cellStyle.Font)
Dim x As Integer = cellBounds.X + (cellBounds.Width - sz.Width)
Dim y As Integer = cellBounds.Y + ((cellBounds.Height - sz.Height) \ 2)
If (cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected Then
graphics.FillRectangle(New SolidBrush(SystemColors.ActiveCaption), cellBounds)
graphics.DrawRectangle(New Pen(SystemColors.ActiveBorder), cellBounds)
graphics.DrawString(Me.Value, cellStyle.Font, New SolidBrush(SystemColors.ActiveCaptionText), x, y)
Else
graphics.FillRectangle(New SolidBrush(cellStyle.BackColor), cellBounds)
graphics.DrawRectangle(New Pen(SystemColors.InactiveBorder), cellBounds)
graphics.DrawString(Me.Value, cellStyle.Font, New SolidBrush(cellStyle.ForeColor), x, y)
End If
End If
Else
'default painting:
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, _
cellStyle, advancedBorderStyle, paintParts)
End If
End If
End Sub
End Class