在c# 自定义控件TextBox里,如何在DELETE之后光标停在删除位置(我遇到的问题是DELETE之后光标自动跳到末尾去了)?
跪求高人解决!
谢谢!
跪求高人解决!
谢谢!
解决方案 »
- 求一进位算法或序列号生成算法
- 学.net的同时在学习php会不会造成时间不够?
- AsyncDelete和EndInvoke的疑惑
- 第1次提问:一个带参数的函数对其参数性质类型及反回的值有什么规定吗?
- 关于CLR、CTS,基本的类库等的问题,希望大家能帮忙
- 求助,日期问题,在线等。。。。
- 超简单问题,是关于函数返值的(在线等)
- 如何将下面这段代码做成类
- 求救! 如何在.net下调用媒体控件?
- 救命啊。。。我怎么xml的数据无法导入到sqlserver中啊?(有代码)
- 请教一个自动下载的问题
- 请问datetimerpicker控件中的日期能不能设置成不同的背景色,例如把节日设置成绿色,其它的为白色.
{
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;
}
}
如果等用户输入完成后,在textchange事件加千位分隔。用户做修改删除都没问题,等操作完成后才处理。
贴你的事件处理代码看看吧。
{
return strMoneyTextBox.ToString("#,#");
} private void MoneyTextBox_TextChanged(object sender, EventArgs e)
{
this.SelectionStart = this.Text.Length;
Check();
string money = this.Text.ToString(); if (this.Text.Length > 1)
{
this.Text = ToMoneyFormat(Convert.ToDecimal(this.Text));
}
if (System.Text.Encoding.GetEncoding(54936).GetByteCount(money) > 32767)
//if (Convert.ToDecimal(System.Text.Encoding.Unicode.GetCharCount(money)) > 32767)
{
this.Enabled = false;
}
} private void MoneyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.Text == "0")
{
this.Text = "";
} if (e.KeyChar > 31 && e.KeyChar != 45 && e.KeyChar < '0' || e.KeyChar > '9')
{
if (this.Text.Length == 0)
{
this.ForeColor = Color.Black;
this.BackColor = Color.White;
this.Text = "0";
}
e.Handled = true;
}
else
{
this.ForeColor = CustomForeColor;
this.BackColor = CustomBackColor;
}
if ((e.KeyChar == 45 && this.SelectionStart != 0))
{
e.Handled = true;
}
} private void MoneyTextBox_KeyUp(object sender, KeyEventArgs e)
{
Check();
if (this.Text.Length == 0)
{
this.Text = "0";
}
} private void Check()
{ this.ForeColor = CustomForeColor;
this.BackColor = CustomBackColor;
if (this.Text == "" || this.Text == "0")
{
this.ForeColor = Color.Black;
this.BackColor = Color.White;
}
else
{
try
{
if (this.Text.ToString().StartsWith(","))
{
this.Text = this.Text.Substring(1, this.Text.Length - 1);
}
if (this.Text != "-")
{
Convert.ToDecimal(this.Text);
} }
catch
{
this.ForeColor = Color.Black;
this.BackColor = Color.White;
this.Text = "0";
}
}
} 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);
}