VB.NET中如何实现句子首字母大写,其余字母小写?
- 内容介绍
- 文章标签
- 相关推荐
本文共计569个文字,预计阅读时间需要3分钟。
我使用了一个简单的函数来判断输入字符是否需要转换为大写。如果前一个字符是空格或者文本框为空,那么输入的字符就会转换为大写。以下是修改后的代码:
csharpStatic PreviousLetter As CharIf PreviousLetter= Or TextBox1.Text.Length=0 Then e.KeyChar=Char.ToUpper(e.KeyChar)End IfPreviousLetter=e.KeyChar
结果为:Good Night Every Body
如何将句子中的替换掉:
csharpTextBox1.Text=TextBox1.Text.Replace(, ).Replace(, );
我用这个:Static PreviousLetter As Char If PreviousLetter = " "c Or TextBox1.Text.Length = 0 Then e.KeyChar = Char.ToUpper(e.KeyChar) End If PreviousLetter = e.KeyChar
但结果总是:
Good Night Every Body
我如何才能将句子中的第一个字母大写,而将其他字母保持正常?我想要的结果是:
Good night every body 不要使用静态变量来保存前一个char.一般而言,这不是必需的和不好的做法.
然后,虽然从您的问题中有点不清楚,假设您希望对TextBox1的文本执行更改,您可能希望在更改后将文本设置回TextBox.
所以解决方案可能如下所示:
If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1) ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf
如果你想大写第一个字母并强制小写其余的你可以修改上面的代码,如下所示:
If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower() ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf
UPDATE
根据评论,如果您想要即时进行此更改(即,当用户在TextBox中键入时),您还需要操作光标.基本上,您需要在更改文本之前存储光标位置,然后在更改后恢复位置.
此外,我将在KeyUp事件中执行这些更改,而不是KeyPress事件. KeyUp在TextBox注册了响应按键的更改后发生.
Dim startPos as Integer Dim selectionLength as Integer ' store the cursor position and selection length prior to changing the text startPos = TextBox1.SelectionStart selectionLength = TextBox1.SelectionLength ' make the necessary changes If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower() ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf ' restore the cursor position and text selection TextBox1.SelectionStart = startPos TextBox1.SelectionLength = selectionLength
本文共计569个文字,预计阅读时间需要3分钟。
我使用了一个简单的函数来判断输入字符是否需要转换为大写。如果前一个字符是空格或者文本框为空,那么输入的字符就会转换为大写。以下是修改后的代码:
csharpStatic PreviousLetter As CharIf PreviousLetter= Or TextBox1.Text.Length=0 Then e.KeyChar=Char.ToUpper(e.KeyChar)End IfPreviousLetter=e.KeyChar
结果为:Good Night Every Body
如何将句子中的替换掉:
csharpTextBox1.Text=TextBox1.Text.Replace(, ).Replace(, );
我用这个:Static PreviousLetter As Char If PreviousLetter = " "c Or TextBox1.Text.Length = 0 Then e.KeyChar = Char.ToUpper(e.KeyChar) End If PreviousLetter = e.KeyChar
但结果总是:
Good Night Every Body
我如何才能将句子中的第一个字母大写,而将其他字母保持正常?我想要的结果是:
Good night every body 不要使用静态变量来保存前一个char.一般而言,这不是必需的和不好的做法.
然后,虽然从您的问题中有点不清楚,假设您希望对TextBox1的文本执行更改,您可能希望在更改后将文本设置回TextBox.
所以解决方案可能如下所示:
If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1) ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf
如果你想大写第一个字母并强制小写其余的你可以修改上面的代码,如下所示:
If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower() ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf
UPDATE
根据评论,如果您想要即时进行此更改(即,当用户在TextBox中键入时),您还需要操作光标.基本上,您需要在更改文本之前存储光标位置,然后在更改后恢复位置.
此外,我将在KeyUp事件中执行这些更改,而不是KeyPress事件. KeyUp在TextBox注册了响应按键的更改后发生.
Dim startPos as Integer Dim selectionLength as Integer ' store the cursor position and selection length prior to changing the text startPos = TextBox1.SelectionStart selectionLength = TextBox1.SelectionLength ' make the necessary changes If TextBox1.TextLength > 1 Then TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower() ElseIf TextBox1.TextLength = 1 Then TextBox1.Text = TextBox1.Text.ToUpper() EndIf ' restore the cursor position and text selection TextBox1.SelectionStart = startPos TextBox1.SelectionLength = selectionLength

