问题
I have a datagridview where DataNames can be entered in a textbox column.I restrict the input length of this column to 6 characters by using the MaxInputLength
property of the DataGridViewTextBoxColumn
.
Here, I want to explain my problem step by step.
1. I wrote Double Byte Characters(eg.1234567890) on a notepad and copy it.Then I went to this DataGridViewTextBox
,Right Click and then choosed Paste
.The DataGridViewTextBox
showed 123456.
2.I wrote Double Byte Characters(eg.123456) on a notepad and copy it.Then I went to this DataGridViewTextBox
,Right Click and then choosed Paste
.The DataGridViewTextBox
showed 123456.
So,MaxInputLength
property only restrict to the input character length( not caring single byte or double byte).
I want to show only 123(6 bytes).
Is there a property or a way to restrict the byte character length especially in Paste operation?
Thanks in advance.
回答1:
I guess you could just handle it in the TextChangedEvent
Something like:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBytes = Encoding.UTF8.GetBytes(textBox1.Text);
var textByteCount = Encoding.UTF8.GetByteCount(textBox1.Text);
var textCharCount = Encoding.UTF8.GetCharCount(textBytes);
if (textCharCount != textByteCount && textByteCount >= 12)
{
textBox1.Text = Encoding.UTF32.GetString(Encoding.UTF32.GetBytes(textBox1.Text), 0, 12);
}
else if (textBox1.Text.Length >= 6)
{
textBox1.Text = textBox1.Text.Substring(0, 6);
}
}
来源:https://stackoverflow.com/questions/14888488/how-to-set-max-byte-length-of-datagridview-when-paste