如何利用C#中的TextChanged事件大写textBox中的第一个字母? [英] how to capitalize the first letter in textBox with TextChanged event in C#?

查看:522
本文介绍了如何利用C#中的TextChanged事件大写textBox中的第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将textBox文本中的每个首字母大写,我希望当用户开始输入文本时,它会自动将首字母大写...

我试过这个 - :

I want to capitalize every first letter in a textBox text and I want when the user start entering the text it will automatically capitalize first letter...
I tried this -:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            textBox1.Text= textInfo.ToTitleCase(textBox1.Text);
        }



但是当第一个字母大写时,它将光标位置设置在文本的开头,以便文本反转..

如果我想输入jayanta它显示Atnayaj但是正确的是Jayanta..



我想要它,就像我们将CharacterCasing属性设置为UPPER然后它将文本更改为大写而不更改textBox中的光标位置...



如何修复它?

提前谢谢....


but when the first letter capitalized it set the cursor position at the beginning of the text so the text is reversed..
If I want to enter "jayanta" it shows "Atnayaj" But correct is "Jayanta"..

I want it like, when we set CharacterCasing property to UPPER then it change the text lower to upper case without changing the cursor position in the textBox...

How to fix it??
thanks in advance....

推荐答案

将此代码用于textChanged!希望对你有帮助!

use this code into textChanged ! hope help you !
char[] v=textBox1.Text.ToCharArray();
string s = v[0].ToString().ToUpper();
for (int b = 1; b < v.Length; b++)
     s += v[b].ToString().ToLower();
textBox1.Text = s;
textBox1.Select(textBox1.Text.Length,0);


这篇关于如何利用C#中的TextChanged事件大写textBox中的第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆