- PR -

DataGridViewでの入力バイト数制限(SJIS)

投稿者投稿内容
KI
大ベテラン
会議室デビュー日: 2007/01/10
投稿数: 239
投稿日時: 2007-01-23 00:20
引用:

コード:
ctl.MaxByteLength = DirectCast(Me.OwningColumn, MaxByteLengthDataGridViewTextBoxColumn).MaxByteLength


と、強制的にキャストしてしまっても良いのでしょうか?
(領域破壊にならないでしょうか?)




お使いのDataGridViewCellのサブクラスがMaxByteLengthDataGridViewTextBoxColumnでのみ
使用されるという前提なら問題ないのではないかと思います。
その前に、そのコードで上手くいったのでしょうか?
くろねこ
会議室デビュー日: 2007/01/22
投稿数: 5
投稿日時: 2007-01-23 09:31
KI様、ありがとうございます。

MyDataGridViewTextBoxColumnクラスのCloneをオーバーライドして
無事、思い通りの動作を得られました。

これにて解決とさせて頂きます。
ありがとうございました。

最終的なコードは以下のようになりました。

コード:


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





[ メッセージ編集済み 編集者: くろねこ 編集日時 2007-01-23 09:36 ]
KI
大ベテラン
会議室デビュー日: 2007/01/10
投稿数: 239
投稿日時: 2007-01-23 22:29
なるほど。ヘルプ確認しました。
MyDataGridViewTextBoxColumnのサブクラスでプロパティを追加するときは
Cloneメソッドをオーバーライドしてコピーしてやる必要があるのですね。
私も勉強になりました。ありがとうございます。

スキルアップ/キャリアアップ(JOB@IT)