add a new property for an exist class

emshahram

Member
Joined
Oct 15, 2006
Messages
6
Programming Experience
5-10
How can i add a property for an exist class? for example i want add RightToLeft property for DataGridViewTextBoxCell class.
Thanks
 
Inherit it and write the property.
VB.NET:
Public Class myDataGridViewTextBoxCell
    Inherits DataGridViewTextBoxCell

    Private _RightToLeft As System.Windows.Forms.RightToLeft = Windows.Forms.RightToLeft.Inherit

    Public Property RightToLeft() As System.Windows.Forms.RightToLeft
        Get
            Return _RightToLeft
        End Get
        Set(ByVal value As System.Windows.Forms.RightToLeft)
            _RightToLeft = value
        End Set
    End Property
End Class
 
inherit a cell of datagridview from textbox control

Hello
I use from this property but when i change this propert to Yes or No convert to inherit and not set.
There is this property in TextBox Control, How can i inherit a cell of datagridview from textbox control.
Thanks
 
The DataGridViewTextBoxCell hosts a DataGridViewTextBoxEditingControl which inherits Textbox, but AFAICS the hosted control is not available. This also is not relevant in this context because the editing control only displays when the cell is in edit mode, else it is the DataGridViewCell that is displaying. What you have to do is use the myDataGridViewTextBoxCell and Paint it yourself if that property is Yes. Still you also have to make the full employment of custom hosting editing control, because in edit mode the Textbox takes over and you have to control it to enable RTL also here. Here is a the documentation article with code that shows how to do this. http://msdn2.microsoft.com/en-us/library/7tas5c80.aspx I'll see if I can post a sample making all this work together.
 
Here is the sample, it's not working perfectly with the property, but does work for the requested RTL feature. It seems that when the datagridview column is initialized the instance property value is lost and a parameterless constructor is used to set the celltemplate based on the type instead. This means if you change RTL property it will not change the column behaviour, but as dedicated RTL column it works. As described in previous post the inherited DataGridViewTextBoxCell is user drawn and the hosted DataGridViewEditingControl draws itself when in editing mode (I found this controls RTL property could be set from Cell, no need to inherit the DataGridViewEditingControl itself for a textbox). The class:
VB.NET:
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
To test add a DataGridView control to a form and add code like this:
VB.NET:
DataGridView1.Columns.Add(New DataGridViewColumn(New tbCellRTL))
DataGridView1.RowCount = 5
For Each row As DataGridViewRow In DataGridView1.Rows
    row.Cells(0).Value = row.Index.ToString
Next row
 
Back
Top