C# 如何将文本框插入符号向右移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1177982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move the textbox caret to the right
提问by monkey_boys
I would like to change all the characters entered into a textbox to upper case. The code will add the character, but how do I move the caret to the right?
我想将输入到文本框中的所有字符更改为大写。代码将添加字符,但如何将插入符号向右移动?
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
textBox3.Text += e.KeyChar.ToString().ToUpper();
e.Handled = true;
}
采纳答案by Fredrik M?rk
Set the CharacterCasing
property of the TextBox
to Upper
; then you don't need to process it manually.
将 的CharacterCasing
属性设置TextBox
为Upper
; 那么你不需要手动处理它。
Note that textBox3.Text += e.KeyChar.ToString().ToUpper();
will append the new character to the end of the string even if the input caret is in the middle of the string(which most users will find highly confusing). For the same reason we cannot assume that the input caret should appear at the end of the string after inputting the character.
请注意,即使输入插入符号位于字符串的中间,textBox3.Text += e.KeyChar.ToString().ToUpper();
也会将新字符附加到字符串的末尾(大多数用户会发现这很容易混淆)。出于同样的原因,我们不能假设输入插入符应该出现在输入字符后的字符串末尾。
If you would still really want to do this in code, something like this should work:
如果您仍然真的想在代码中执行此操作,则应该执行以下操作:
// needed for backspace and such to work
if (char.IsControl(e.KeyChar))
{
return;
}
int selStart = textBox3.SelectionStart;
string before = textBox3.Text.Substring(0, selStart);
string after = textBox3.Text.Substring(before.Length);
textBox3.Text = string.Concat(before, e.KeyChar.ToString().ToUpper(), after);
textBox3.SelectionStart = before.Length + 1;
e.Handled = true;
回答by Binary Worrier
This will preserve the location of the insertion point (but persionally I'd go with the answer given by Fredrik M?rk)
这将保留插入点的位置(但我个人会采用 Fredrik M?rk 给出的答案)
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
int selStart = textBox3.SelectionStart;
textBox3.Text += e.KeyChar.ToString().ToUpper();
textBox3.SelectionStart = selStart;
e.Handled = true;
}
SelectionStart might actually be called SelStart, I don't have a compiler handy at the moment.
SelectionStart 实际上可能被称为 SelStart,我目前手边没有编译器。
回答by erelender
If you have to do this manually, you can use
如果您必须手动执行此操作,则可以使用
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
textBox3.Text += e.KeyChar.ToString().ToUpper();
textBox3.SelectionStart = textBox3.Text.Length;
e.Handled = true;
}
But the preceding code inserts the new character at the end of the text. If you want to insert it at where the cursor is:
但是前面的代码在文本的末尾插入了新字符。如果要将其插入到光标所在的位置:
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
int selStart = textBox3.SelectionStart;
textBox3.Text = textBox3.Text.Insert(selStart,e.KeyChar.ToString().ToUpper());
textBox3.SelectionStart = selStart + 1;
e.Handled = true;
}
This code inserts the new character at the cursor position and moves the cursor to the left of the newly inserted character.
此代码在光标位置插入新字符并将光标移动到新插入字符的左侧。
But i still think setting CharacterCasing is better.
但我仍然认为设置 CharacterCasing 更好。
回答by Chris Dunaway
Another method is to just change the value of the KeyChar itself:
另一种方法是更改 KeyChar 本身的值:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
if ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) {
e.KeyChar = (char)((int)e.KeyChar & 0xDF);
}
}
Although, using the CharacterCasing property is the easiest solution.
虽然,使用 CharacterCasing 属性是最简单的解决方案。
回答by jdc
tbNumber.SelectionStart = tbNumber.Text.ToCharArray().Length;
tbNumber.SelectionLength = 0;
回答by just nothing
private void txtID_TextChanged(object sender, EventArgs e)
{
txtID.Text = txtID.Text.ToUpper();
txtID.SelectionStart = txtID.Text.Length;
}