protected override void WndProc(ref Message m)
{
MaxLength = this._maxByteLength;
const int mPaste = 0x0302;
if (m.Msg == mPaste)
{
IDataObject iData = Clipboard.GetDataObject(); //取剪贴板对象
if (iData.GetDataPresent(DataFormats.Text)) //判断是否是Text
{
string text = (string)iData.GetData(DataFormats.Text);//取数据
char[] str = text.ToCharArray();
if (CheckByteLengthFlow(text))
{
int len = GetByteLength(text); //输入的字符的长度
int tlen = GetByteLength(this.Text); //文本框原有文本的长度
int slen = GetByteLength(this.SelectedText); //文本框选中文本的长度
char[] c = text.ToCharArray();
int g;
int f = 0;
int count = 0;
for (g = 0; g < c.Length; g++)
{
count += GetByteLength(c[g].ToString());
if (_maxByteLength < (tlen - slen + count))
{ f = g; break; }
}
int j = this.Text.ToCharArray().Length;
int k = this.SelectedText.ToCharArray().Length;
this.MaxLength = j + f - k;
//}
}
}
}
base.WndProc(ref m);
}
private bool CheckByteLengthFlow(string text)
{
int len = GetByteLength(text); //输入的字符的长度
int tlen = GetByteLength(this.Text); //文本框原有文本的长度
int slen = GetByteLength(this.SelectedText); //文本框选中文本的长度
return (MaxLength < len + tlen - slen);
}
private int GetByteLength(string vtext)
{
return System.Text.Encoding.GetEncoding("shift_jis").GetByteCount(vtext);
}
namespace MoneyText
{
public partial class MoneyTextBox : TextBox
{
public MoneyTextBox()
{
InitializeComponent();
}
private int _maxByteLength = 32767;
private Boolean _checkMinusDataEnable = true;
private Color _minusDataColor = Color.Red;
private Boolean _changeForeColorEnable = true;
private Color _customForeColor = Color.Red;
private Boolean _changeBackColorEnable = false;
private Color _customBackColor = Color.FromArgb(255, 255, 128);
public Color MinusDataColor
{
get
{
return _minusDataColor;
}
set
{
_minusDataColor = value;
}
}
public Boolean CheckMinusDataEnable
{
set
{
_checkMinusDataEnable = value;
}
}
public int MaxByteLength
{
get
{
return _maxByteLength;
}
set
{
_maxByteLength = value;
}
}
public Boolean ChangeBackColorEnable
{
set
{
_changeBackColorEnable = value;
}
}
public Color CustomForeColor
{
get
{
return _customForeColor;
}
set
{
_customForeColor = value;
}
}
public Color CustomBackColor
{
get
{
return _customBackColor;
}
set
{
_customBackColor = value;
}
}
public Boolean ChangeForeColorEnable
{
set
{
_changeForeColorEnable = value;
}
}