mon*_*oys 8 c# textbox keypress
我想将输入文本框的所有字符更改为大写.代码将添加字符,但如何将插入符号移到右侧?
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
textBox3.Text += e.KeyChar.ToString().ToUpper();
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
Fre*_*örk 18
设置to 的CharacterCasing属性; 那么你不需要手动处理它.TextBoxUpper
请注意,即使输入插入符位于字符串的中间(大多数用户会发现高度混乱),textBox3.Text += e.KeyChar.ToString().ToUpper();也会将新字符附加到字符串的末尾.出于同样的原因,我们不能假设在输入字符后输入插入符应出现在字符串的末尾.
如果您仍然希望在代码中执行此操作,则此类应该可以正常工作:
// 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;
Run Code Online (Sandbox Code Playgroud)
小智 12
tbNumber.SelectionStart = tbNumber.Text.ToCharArray().Length;
tbNumber.SelectionLength = 0;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15472 次 |
| 最近记录: |