PDA

View Full Version : سوال: حرفه‌ای: پاس کردن یک Property از DataGridViewColumn به یک کلاس دیگر (همراه نمونه سورس کد)



gilsoft
جمعه 01 اسفند 1393, 21:06 عصر
سلام دوستان

ابتدا کدهای زیر رو ملاحظه بفرمائید:

Imports System.ComponentModel


Public Class DgvNumericColumn
Inherits DataGridViewColumn


Public Sub New()
MyBase.New(New DgvNumericCell())
Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
Me.DefaultCellStyle.Padding = New System.Windows.Forms.Padding(4, 0, 4, 0)
End Sub


Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a NumericCell.
If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(DgvNumeri cCell)) Then Throw New InvalidCastException("Must be a DgvNumericCell")
MyBase.CellTemplate = value
End Set
End Property


Class DgvNumericCell
Inherits DataGridViewTextBoxCell


Public Sub New()
' Use the short date format.
End Sub


Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As DgvNumericEditingControl = CType(DataGridView.EditingControl, DgvNumericEditingControl)
ctl.Text = Me.Value.ToString
End Sub


Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that NumericCell uses.
Return GetType(DgvNumericEditingControl)
End Get
End Property


Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that NumericCell contains.
Return GetType(String)
End Get
End Property


Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return ""
End Get
End Property
End Class


Class DgvNumericEditingControl
Inherits DgvNumericNBox
Implements IDataGridViewEditingControl


Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer


Public Sub New()


End Sub


Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormatte dValue
Get
Return Me.Text
End Get
Set(ByVal value As Object)
If TypeOf value Is [String] Then Me.Text = CStr(value)
End Set
End Property


Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlForma ttedValue
Return Me.Text
End Function


Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditin gControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor 'Color.LemonChiffon
End Sub


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


Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInp utKey
Return True
End Function


Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlF orEdit
' No preparation needs to be done.
End Sub


Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingContr olOnValueChange
Get
Return False
End Get
End Property


Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGrid View
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property


Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueCha nged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property


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


Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDir ty(True)
MyBase.OnTextChanged(e)
End Sub
End Class


Class DgvNumericNBox
Inherits DgvNBox


Sub New()
Me.ShowZeroValue = TriState.False
End Sub


Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_CHAR As Integer = &H102


Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCode
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter Then msg.WParam = Keys.Tab
Return (MyBase.PreProcessMessage(msg))
End Function


End Class


Class DgvNBox
Inherits System.Windows.Forms.TextBox


#Region " - Initialize - "
Public Enum eRequiered
No = 0
Yes = 1
End Enum


Private ErrPrv As New ErrorProvider
Private _BackColorMain, _ForeColorMain As Color : Private _FontMain As Font
Private _Validating As Boolean


Private _FocusBackColor As Color = Color.White
Private _FocusForeColor As Color = Color.Black
Private _FocusFontBold As Boolean = False
Private _FocusValueSelected As Boolean = True


Private _Requiered As eRequiered = eRequiered.No
Private _SeparatorChar As Char = ","c
Private _EnterToTab As Boolean = True
Private _EscToClear As Boolean = True
Private _ShowZeroValue As TriState = TriState.UseDefault
Private _UseParenthesesForNegativeNumbers As TriState = TriState.UseDefault
Private _GroupDigits As TriState = TriState.UseDefault


Private _minValue As Long = CLng(0)
Private _maxValue As Long = Long.MaxValue
Private _Value As Long = 0


Sub New()


Me.ShortcutsEnabled = False
Me.Size = New Point(113, 21)
With ErrPrv
.SetIconAlignment(Me, ErrorIconAlignment.MiddleLeft)
.SetIconPadding(Me, 5)
.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
.RightToLeft = True
End With
End Sub
#End Region


#Region " - Properties - "


<DefaultValue(GetType(Color), "White")> _
Public Property FocusBackColor() As Color
Get
Return _FocusBackColor
End Get
Set(ByVal value As Color)
_FocusBackColor = value
End Set
End Property 'FocusBackColor As Color


<DefaultValue(GetType(Color), "Black")> _
Public Property FocusForeColor() As Color
Get
Return _FocusForeColor
End Get
Set(ByVal value As Color)
_FocusForeColor = value
End Set
End Property 'FocusForeColor As Color


<DefaultValue(False)> _
Public Property FocusFontBold() As Boolean
Get
Return _FocusFontBold
End Get
Set(ByVal value As Boolean)
_FocusFontBold = value
End Set
End Property 'FocusFontBold As Boolean


<DefaultValue(True)> _
Public Property FocusValueSelected() As Boolean
Get
Return _FocusValueSelected
End Get
Set(ByVal value As Boolean)
_FocusValueSelected = value
End Set
End Property 'FocusValueSelected As Boolean


<DefaultValue(GetType(eRequiered), "No")> _
Public Property Requiered() As eRequiered
Get
Return _Requiered
End Get
Set(ByVal value As eRequiered)
_Requiered = value
End Set
End Property


<DefaultValue(","c)> _
Public Property SeparatorChar() As Char
Get
Return _SeparatorChar
End Get
Set(ByVal value As Char)
_SeparatorChar = value
End Set
End Property


<DefaultValue(True)> _
Public Property EnterToTab() As Boolean
Get
Return _EnterToTab
End Get
Set(ByVal value As Boolean)
_EnterToTab = value
End Set
End Property


<DefaultValue(True)> _
Public Property EscToClear() As Boolean
Get
Return _EscToClear
End Get
Set(ByVal value As Boolean)
_EscToClear = value
End Set
End Property


<DefaultValue(GetType(TriState), "UseDefault")> _
Public Property ShowZeroValue() As TriState
Get
Return _ShowZeroValue
End Get
Set(ByVal value As TriState)
_ShowZeroValue = value
End Set
End Property


<DefaultValue(GetType(TriState), "UseDefault")> _
Public Property UseParenthesesForNegativeNumbers() As TriState
Get
Return _UseParenthesesForNegativeNumbers
End Get
Set(ByVal value As TriState)
_UseParenthesesForNegativeNumbers = value
End Set
End Property


<DefaultValue(GetType(TriState), "UseDefault")> _
Public Property GroupDigits() As TriState
Get
Return _GroupDigits
End Get
Set(ByVal value As TriState)
_GroupDigits = value
If GroupDigits = TriState.False Then
Me.Text = _Value.ToString("#")
Else
Me.Text = _Value.ToString("#,#")
End If
End Set
End Property


<DefaultValue(GetType(Long), "0")> _
Public Property MinValue() As Long
Get
Return _minValue
End Get
Set(ByVal value As Long)
_minValue = value
End Set
End Property


<DefaultValue(GetType(Long), "9223372036854775807")> _
Public Property MaxValue() As Long
Get
Return _maxValue
End Get
Set(ByVal value As Long)
_maxValue = value
End Set
End Property


<DefaultValue(GetType(Long), "0")> _
Public Property Value() As Long
Get
Return (GetValue(Me.Text))
End Get
Set(ByVal value As Long)
_Value = value
If GroupDigits Then
Me.Text = _Value.ToString("#,#")
Else
Me.Text = _Value.ToString("#")
End If
End Set
End Property
#End Region


#Region " - Events - "


Protected Overrides Function ProcessKeyMessage(ByRef m As System.Windows.Forms.Message) As Boolean
If Me.RightToLeft = Windows.Forms.RightToLeft.Yes Then
Select Case m.WParam
Case Keys.Left : m.WParam = Keys.Right
Case Keys.Right : m.WParam = Keys.Left
End Select
End If
Return (MyBase.ProcessKeyMessage(m))
End Function 'ProcessKeyMessage


Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)


Dim uc As Globalization.UnicodeCategory = Char.GetUnicodeCategory(e.KeyChar)
Select Case uc
Case Globalization.UnicodeCategory.DecimalDigitNumber, Globalization.UnicodeCategory.Control
_Validating = True
Case Else
e.Handled = True
End Select
End Sub 'OnKeyPress


Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)


Dim kc As Keys = e.KeyCode
Select Case kc
Case Keys.Enter : e.SuppressKeyPress = True
Case Keys.Escape : If _EscToClear Then Me.Text = ""
Case Keys.Delete : SendKeys.Send("{BKSP}")
Case Keys.Add, Keys.Oemplus : Me.Text = CStr(Val(Me.Text.Trim) + 1)
Case Keys.Subtract, Keys.OemMinus : Me.Text = CStr(Val(Me.Text.Trim) - 1)
End Select


End Sub 'OnKeyDown


Protected Overrides Sub OnEnter(e As System.EventArgs)
MyBase.OnEnter(e)


_Validating = True
_ForeColorMain = Me.ForeColor : _BackColorMain = Me.BackColor : _FontMain = Me.Font
Me.ForeColor = _FocusForeColor : Me.BackColor = _FocusBackColor
If _FocusFontBold Then Me.Font = New System.Drawing.Font(Me.Font.Name.ToString, Me.Font.Size!, System.Drawing.FontStyle.Bold)
If _FocusValueSelected Then Me.SelectAll() Else Me.Select(False, False)
End Sub 'OnEnter


Protected Overrides Sub OnLeave(e As System.EventArgs)
MyBase.OnLeave(e)


If (_Value < _minValue Or _Value > _maxValue) Then
If _Requiered = eRequiered.Yes Then
ErrPrv.SetError(Me, "داده نامعتبر (خارج از محدوده) !")
My.Computer.Audio.PlaySystemSound(System.Media.Sys temSounds.Hand)
Me.Focus()
Else
ErrPrv.SetError(Me, "")
If _Value = 0 Then
End If
End If
End If
Me.ForeColor = _ForeColorMain : Me.BackColor = _BackColorMain : Me.Font = _FontMain
End Sub 'OnLeave


Protected Overrides Sub OnTextChanged(e As System.EventArgs)
MyBase.OnTextChanged(e)


If DesignMode = False Then
If Me.TextLength > 0 Then
If CDec(GT(Me.Text.Trim)) >= _minValue And CDec(GT(Me.Text.Trim)) <= _maxValue Then
If _SeparatorChar = "," Then
Me.Text = FormatNumber(CLng(Me.Text.Trim), 0, _ShowZeroValue, _UseParenthesesForNegativeNumbers, _GroupDigits)
Else
Me.Text = (Replace(FormatNumber(CLng(GT(Me.Text)), 0, _ShowZeroValue, _UseParenthesesForNegativeNumbers, _GroupDigits), ",", _SeparatorChar))
End If
End If
Me.SelectionStart = Me.TextLength
End If
End If
End Sub 'OnTextChanged


#End Region


#Region " - Methods - "


Private Function GT(txt As String) As String
Return (Replace(txt.Trim, _SeparatorChar, ""))
End Function 'فرخوانی تکس باکس بصورت عددی


Private Function GetValue(Optional ByVal num As String = "") As Long
num = num.Trim : Dim tmpValue As Long = 0
If String.IsNullOrEmpty(num) Then
If Not String.IsNullOrEmpty(Me.Text.Trim) Then tmpValue = CLng(Me.Text.Replace(_SeparatorChar, ""))
Else
tmpValue = CLng(num.Trim.Replace(_SeparatorChar, ""))
End If
_Value = tmpValue
Return (tmpValue)
End Function ' GetValue As Long فراخوانی مقدار جاری کنترل بدون هرگونه کارکتر اضافه


#End Region


End Class


End Class 'DgvNumericColumn


من میخوام Property زیر رو در کلاس DgvNumericColumn اضافه کنم ... و مقدار اونو در کلاس DgvNumericCell .. سابروتین InitializeEditingControl بعد از سطر 40 بصورت:
ctl.GroupDigits = خاصیت مورد نظر

فراخوانی کنم .. اما نمی‌دونم چه جوری :متفکر: :متفکر: :متفکر:


Public _GroupDigits As TriState = TriState.UseDefault


<DefaultValue(GetType(TriState), "UseDefault")> _
Public Property GroupDigits() As TriState
Get
Return _GroupDigits
End Get
Set(ByVal value As TriState)
_GroupDigits = value
End Set
End Property

از اساتید حرفه‌ای ممنون میشم که: یه راهنمایی بفرمایند ....

gilsoft
جمعه 01 اسفند 1393, 21:24 عصر
سلام مجدد

یادم رفت که عرض کنم:
این DataGridViewColumn فقط عدد میگیره و هنگام گرفتن عدد (همزمان) سه رقم سه رقم هم جدا می‌کنه

حالا من مبخوام در بعضی از ستونها (‌ مثلا ستون تعدادِ یه فاکتور ) سه‌رقم سه‌رقم جدا نکنه !

در واقع این Property هم برای Option همین موضوع میخوام استفاده کنم ...


کدهای پست 1# هم تو همین پست میزارم تا مشکلی پیش نیاد

ممنونم که وقت گذاشتید .....

gilsoft
شنبه 02 اسفند 1393, 22:18 عصر
سلام دوستان

مشکل برطرف شد ....

بزودی فایل ضمیمه پست 2# رو Update می‌کنم ...

از همه‌ی دوستان سپاسگزارم ....