C#文本框光标定位

前端之家收集整理的这篇文章主要介绍了C#文本框光标定位前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我觉得我刚刚错过了一个简单的属性,但是你可以将光标设置到文本框中的一行的末尾吗?
  1. private void txtNumbersOnly_KeyPress(object sender,KeyPressEventArgs e)
  2. {
  3. if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == '-')
  4. {
  5. TextBox t = (TextBox)sender;
  6. bool bHandled = false;
  7. _sCurrentTemp += e.KeyChar;
  8.  
  9. if (_sCurrentTemp.Length > 0 && e.KeyChar == '-')
  10. {
  11. // '-' only allowed as first char
  12. bHandled = true;
  13. }
  14.  
  15. if (_sCurrentTemp.StartsWith(Convert.ToString('.')))
  16. {
  17. // add '0' in front of decimal point
  18. t.Text = string.Empty;
  19. t.Text = '0' + _sCurrentTemp;
  20. _sCurrentTemp = t.Text;
  21. bHandled = true;
  22. }
  23.  
  24. e.Handled = bHandled;
  25. }

经过’.’测试.作为第一个字符,光标在添加的文本之前.所以,而不是“0.123”,结果是“1230”.没有自己移动光标.

如果这是一个重复的问题,我也表示歉意.

解决方法

  1. t.SelectionStart = t.Text.Length;

猜你在找的C#相关文章