我觉得我刚刚错过了一个简单的属性,但是你可以将光标设置到文本框中的一行的末尾吗?
- private void txtNumbersOnly_KeyPress(object sender,KeyPressEventArgs e)
- {
- if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == '-')
- {
- TextBox t = (TextBox)sender;
- bool bHandled = false;
- _sCurrentTemp += e.KeyChar;
- if (_sCurrentTemp.Length > 0 && e.KeyChar == '-')
- {
- // '-' only allowed as first char
- bHandled = true;
- }
- if (_sCurrentTemp.StartsWith(Convert.ToString('.')))
- {
- // add '0' in front of decimal point
- t.Text = string.Empty;
- t.Text = '0' + _sCurrentTemp;
- _sCurrentTemp = t.Text;
- bHandled = true;
- }
- e.Handled = bHandled;
- }
经过’.’测试.作为第一个字符,光标在添加的文本之前.所以,而不是“0.123”,结果是“1230”.没有自己移动光标.
如果这是一个重复的问题,我也表示歉意.
解决方法
- t.SelectionStart = t.Text.Length;