6

我用这个:

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
4

7 回答 7

11

不要使用静态变量来保存前一个字符。一般来说,这是不需要的,也是不好的做法。

然后,尽管您的问题有点不清楚,但假设您希望对 的文本进行更改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

更新

根据评论,如果您想即时进行此更改(即当用户在 TextBox 中键入时),那么您还需要操作光标。本质上,您需要在更改文本之前存储光标位置,然后在更改后恢复位置。

另外,我会在事件中执行这些更改,KeyUp而不是在KeyPress事件中。在KeyUpTextBox 注册更改以响应按键按下之后发生。

 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
于 2013-12-31T16:28:34.800 回答
6

TextInfo.ToTitleCase方法 将指定的字符串转换为标题大小写。

txtName.Text = Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(txtName.Text.ToLower)

您必须先将整个文本转换为小写,否则它将无法按预期工作:

(除了完全大写的单词,它们被认为是首字母缩略词)

于 2017-02-06T15:58:58.500 回答
1

你绝对可以做到。

正则表达式是最直接的方式......类似于您正在做的事情,您可以将它们组合起来。

很多人已经看过这个了。这是一个:
使用 C# 格式化字符串中的句子

使用该正则表达式字符串替换而不是您的 Char.ToUpper。
或者只是等到输入所有文本并运行一次,例如当控件失去焦点时。

于 2013-12-31T16:27:58.743 回答
1
' With VB.net you could use the Mid() function also for assignments

Dim s as string = "good evening" 
If Len(s)>0 Then  Mid(s, 1, 1) = UCase(Left(s, 1)) 

'Result: "Good evening"
于 2019-12-20T11:05:57.227 回答
0

尝试这个

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    TextBox1.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
    TextBox1.Select(TextBox1.Text.Length, 0)

    End Sub
于 2019-11-06T15:37:09.110 回答
0

创建一个扩展:

Public Function ToUppercaseFirstLetter(ByVal Text As String) As String
    If Text = "" Then Return Text
    Dim array() As Char = Text.ToCharArray
    array(0) = Char.ToUpper(array(0))
    Return array
End Function
于 2020-08-31T11:09:20.647 回答
-1

try this code

    If Char.IsLetter(e.KeyChar) Then

        If txtType.Text.Length = 0 Then
            e.KeyChar = Char.ToUpper(e.KeyChar)
        Else
            e.KeyChar = Char.ToLower(e.KeyChar)
        End If

    End If
于 2021-01-27T18:13:19.003 回答