TextBoxCell でバイト単位で入力文字数を制限する方法
対象製品
MultiRow for Windows Forms 5.0J
詳細
次のコードは、TextBoxCell で入力可能文字数を6バイトに設定する例です。
[Visual Basic]
[C#]
参考:
[Visual Basic]
Imports System.Text
Imports GrapeCity.Win.MultiRow
Private Sub GcMultiRow1_EditingControlShowing(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
If TypeOf e.Control Is TextBoxEditingControl Then
Dim textBox As TextBoxEditingControl = DirectCast(e.Control, TextBoxEditingControl)
If Not textBox Is Nothing Then
RemoveHandler textBox.TextChanged, AddressOf Me.textBox_TextChanged
AddHandler textBox.TextChanged, AddressOf Me.textBox_TextChanged
End If
End If
End Sub
Private Sub textBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim textBox As TextBox = DirectCast(sender, TextBox)
Dim sjis As Encoding = Encoding.GetEncoding("Shift-JIS")
Dim maxLengthAsByte As Integer = 6
Dim sourceText() As Byte = sjis.GetBytes(textBox.Text.ToCharArray())
If sourceText.Length 'gt; maxLengthAsByte Then
Array.Resize(Of Byte)(sourceText, maxLengthAsByte)
textBox.Text = sjis.GetString(sourceText)
textBox.SelectionStart = textBox.Text.Length
End If
End Sub
Imports GrapeCity.Win.MultiRow
Private Sub GcMultiRow1_EditingControlShowing(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
If TypeOf e.Control Is TextBoxEditingControl Then
Dim textBox As TextBoxEditingControl = DirectCast(e.Control, TextBoxEditingControl)
If Not textBox Is Nothing Then
RemoveHandler textBox.TextChanged, AddressOf Me.textBox_TextChanged
AddHandler textBox.TextChanged, AddressOf Me.textBox_TextChanged
End If
End If
End Sub
Private Sub textBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim textBox As TextBox = DirectCast(sender, TextBox)
Dim sjis As Encoding = Encoding.GetEncoding("Shift-JIS")
Dim maxLengthAsByte As Integer = 6
Dim sourceText() As Byte = sjis.GetBytes(textBox.Text.ToCharArray())
If sourceText.Length 'gt; maxLengthAsByte Then
Array.Resize(Of Byte)(sourceText, maxLengthAsByte)
textBox.Text = sjis.GetString(sourceText)
textBox.SelectionStart = textBox.Text.Length
End If
End Sub
[C#]
using GrapeCity.Win.MultiRow;
private void gcMultiRow1_EditingControlShowing(object sender, GrapeCity.Win.MultiRow.EditingControlShowingEventArgs e)
{
if (e.Control is TextBoxEditingControl)
{
TextBoxEditingControl textBox = e.Control as TextBoxEditingControl;
if (textBox != null)
{
textBox.TextChanged -= new EventHandler(textBox_TextChanged);
textBox.TextChanged += new EventHandler(textBox_TextChanged);
}
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
Encoding sjis = Encoding.GetEncoding("Shift-JIS");
int maxLengthAsByte = 6;
byte[] sourceText = sjis.GetBytes(textBox.Text.ToCharArray());
if (sourceText.Length > maxLengthAsByte)
{
Array.Resize<byte>(ref sourceText, maxLengthAsByte);
textBox.Text = sjis.GetString(sourceText);
textBox.SelectionStart = textBox.Text.Length;
}
}
private void gcMultiRow1_EditingControlShowing(object sender, GrapeCity.Win.MultiRow.EditingControlShowingEventArgs e)
{
if (e.Control is TextBoxEditingControl)
{
TextBoxEditingControl textBox = e.Control as TextBoxEditingControl;
if (textBox != null)
{
textBox.TextChanged -= new EventHandler(textBox_TextChanged);
textBox.TextChanged += new EventHandler(textBox_TextChanged);
}
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
Encoding sjis = Encoding.GetEncoding("Shift-JIS");
int maxLengthAsByte = 6;
byte[] sourceText = sjis.GetBytes(textBox.Text.ToCharArray());
if (sourceText.Length > maxLengthAsByte)
{
Array.Resize<byte>(ref sourceText, maxLengthAsByte);
textBox.Text = sjis.GetString(sourceText);
textBox.SelectionStart = textBox.Text.Length;
}
}
参考:
- InputManCell の GcTextBoxCell を使用すると、バイト単位で入力文字数を簡単に指定できます。(GcTextBoxCell.MaxLengthUnit プロパティ)
関連情報
この文書は、以前は次のFAQ IDで公開されていました : 11280