Recently, we have talked about the Datagrid, which is especially depressing for new users. Why is there no better control to meet their own needs.
In fact, through rewriting, we can achieve many different functional experiences. Here we only discuss the issue of datagridcolumnstyle rewriting.
========================================================== =
Power by: landlordh
Datatime: 2005-08-04
Reprinted please indicate the source, thank you
========================================================== =
1. Override TextBox:
Public Class XP_TextBox
Inherits System. Windows. Forms. TextBox
# Region "Windows"
Public Sub New ()
MyBase. New ()
InitializeComponent ()
End Sub
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
Components. Dispose ()
End If
End If
MyBase. Dispose (disposing)
End Sub
Windows
Private components As System. ComponentModel. IContainer
<System. Diagnostics. DebuggerStepThrough ()> Private Sub InitializeComponent ()
TextBox
Me. EnableContextMenu = True
Me. EnablePaste = True
Me. Name = "TextBox"
End Sub
# End Region
# Region "Variables"
Private m_EnPaste As Boolean = True
Private m_EnContextMenu As Boolean = True
# End Region
# Region "Property"
Property EnablePaste () As Boolean
Get
Return m_EnPaste
End Get
Set (ByVal Value As Boolean)
M_EnPaste = Value
Me. Invalidate ()
End Set
End Property
Property EnableContextMenu () As Boolean
Get
Return m_EnContextMenu
End Get
Set (ByVal Value As Boolean)
M_EnContextMenu = Value
Me. Invalidate ()
End Set
End Property
# End Region
Protected Overrides Sub WndProc (ByRef m As System. Windows. Forms. Message)
Select Case m. Msg
Case & H302 paste
RaiseEvent PasteEvent ()
If Not m_EnPaste Then Return
Case & amp; Consumer B contextmenu
If Not m_EnContextMenu Then Return
End Select
MyBase. WndProc (m)
End Sub
Public Event PasteEvent ()
End Class
2. Override datagridcolumnstyle (important description ):
Imports System. Drawing
Imports System. Windows. Forms
Public NotInheritable Class maid
Inherits System. Windows. Forms. DataGridColumnStyle
# Region "Declare Property"
Private WithEvents m_TextBox As New Landlord. Component. XP_TextBox
Private IsEditing As Boolean
Private EditingRow As Integer =-1
Private m_oldvalue As String
# End Region
# Region "windows"
Sub New ()
Me. m_TextBox.Visible = False
End Sub
Public Sub New (ByVal Container As System. ComponentModel. IContainer)
MyClass. New ()
Container. Add (Me)
End Sub
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
Components. Dispose ()
End If
End If
MyBase. Dispose (disposing)
End Sub
Private components As System. ComponentModel. IContainer
<System. Diagnostics. DebuggerStepThrough ()> Private Sub InitializeComponent ()
Components = New System. ComponentModel. Container
End Sub
# End Region
# Region "Get Function"
Protected Overrides Function GetMinimumHeight () As Integer
Return m_TextBox.PreferredHeight + 2
End Function
Protected Overrides Function GetPreferredHeight (ByVal g As System. Drawing. Graphics, ByVal value As Object) As Integer
Return m_TextBox.PreferredHeight + 2
End Function
Protected Overrides Function GetPreferredSize (ByVal g As System. Drawing. Graphics, ByVal value As Object) As System. Drawing. Size
Return New Size (50, m_TextBox.PreferredHeight + 2)
End Function
# End Region
# Region "Paint"
Protected Overloads Overrides Sub Paint (ByVal g As System. drawing. graphics, ByVal bounds As System. drawing. rectangle, ByVal [source] As System. windows. forms. currencyManager, ByVal rowNum As Integer)
Paint (g, bounds, [source], rowNum, False)
End Sub
Protected Overloads Overrides Sub Paint (ByVal g As System. drawing. graphics, ByVal bounds As System. drawing. rectangle, ByVal [source] As System. windows. forms. currencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
Dim brush_for As Brush = New SolidBrush (Me. DataGridTableStyle. ForeColor)
Dim brush_bak As Brush = New SolidBrush (Me. D