Imports System.Windows.Forms
Imports System.ComponentModel
Namespace CustomCtrl.Windows.Forms
#Region "MyDataGridViewTextBoxColumn"
Public Class MyDataGridViewTextBoxColumn
Inherits System.Windows.Forms.DataGridViewTextBoxColumn
Public Sub New()
Me.MaxByteLength = 65535
Dim cell As New MyDataGridViewTextBoxCell
Me.CellTemplate = cell
End Sub
Private _MaxByteLength As Integer
<Category("動作"), _
DefaultValue(65535), _
Description("コントロールに入力できる最大文字バイト数を指定します。")> _
Public Overridable Property MaxByteLength() As Integer
Get
Return Me._MaxByteLength
End Get
Set(ByVal value As Integer)
If Me._MaxByteLength <> value Then
Me._MaxByteLength = value
End If
End Set
End Property
Public Overrides Function Clone() As Object
Dim oClone As MyDataGridViewTextBoxColumn = MyBase.Clone
oClone.MaxByteLength = Me.MaxByteLength
Return oClone
End Function
End Class
#End Region
#Region "MyDataGridViewTextBoxCell"
Public Class MyDataGridViewTextBoxCell
Inherits System.Windows.Forms.DataGridViewTextBoxCell
Public Sub New()
End Sub
Public Overrides ReadOnly Property EditType() As System.Type
Get
Return GetType(MyDataGridViewTextBoxEditingControl)
End Get
End Property
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As MyDataGridViewTextBoxEditingControl
ctl = CType(DataGridView.EditingControl, MyDataGridViewTextBoxEditingControl)
ctl.MaxByteLength = DirectCast(Me.OwningColumn, MyDataGridViewTextBoxColumn).MaxByteLength
End Sub
End Class
#End Region
#Region "MyDataGridViewTextBoxEditingControl"
Public Class MyDataGridViewTextBoxEditingControl : Inherits System.Windows.Forms.DataGridViewTextBoxEditingControl
Private _MaxByteLength As Integer
Public Overridable Property MaxByteLength() As Integer
Get
Return _MaxByteLength
End Get
Set(ByVal value As Integer)
If _MaxByteLength <> value Then
_MaxByteLength = value
End If
End Set
End Property
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_CHAR As Integer = &H102
Const WM_PASTE As Integer = &H302
Select Case m.Msg
Case WM_CHAR
Dim eKeyPress As New KeyPressEventArgs(Microsoft.VisualBasic.ChrW(m.WParam.ToInt32()))
Me.OnChar(eKeyPress)
If eKeyPress.Handled Then
Return
End If
Case WM_PASTE
If Me.MaxLength * 2 > _MaxByteLength Then
Me.OnPaste(New System.EventArgs())
Return
End If
End Select
MyBase.WndProc(m)
End Sub
Protected Overridable Sub OnChar(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Char.IsControl(e.KeyChar) Then
Return
End If
Dim sjisEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
Dim textByteCount As Integer = sjisEncoding.GetByteCount(Me.Text)
Dim inputByteCount As Integer = sjisEncoding.GetByteCount(e.KeyChar.ToString())
Dim selectedTextByteCount As Integer = sjisEncoding.GetByteCount(Me.SelectedText)
If textByteCount + inputByteCount - selectedTextByteCount > _MaxByteLength Then
e.Handled = True
End If
End Sub
Protected Overridable Sub OnPaste(ByVal e As System.EventArgs)
Dim clipboardText As Object = Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text)
If clipboardText Is Nothing Then
Return
End If
Dim sjisEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
Dim inputText As String = clipboardText.ToString()
Dim textByteCount As Integer = sjisEncoding.GetByteCount(Me.Text)
Dim inputByteCount As Integer = sjisEncoding.GetByteCount(inputText)
Dim selectedTextByteCount As Integer = sjisEncoding.GetByteCount(Me.SelectedText)
Dim remainByteCount As Integer = _MaxByteLength - (textByteCount - selectedTextByteCount)
If remainByteCount <= 0 Then
Return
End If
If remainByteCount >= inputByteCount Then
Me.SelectedText = inputText
Else
Me.SelectedText = inputText.Substring(0, remainByteCount)
End If
End Sub
End Class
#End Region
End Namespace
|