2012-01-18 77 views
-1

我希望我的TextBox将文本输入为Sentence Case(ProperCase) ..但我不想在Lost FocusKeyPress这样的事件中编写任何代码。句柄大小写或文本框中的正确大小写

默认情况下,只要用户在文本框中输入或键入,每个单词的首字母应自动转换为UpperCase

+0

我可能是错的,但我认为唯一的方法是检查文本框的值,因为使用事件输入文本并相应地更新文本。 – 2012-01-18 11:32:38

+0

我已经尝试过使用文化课,但我认为这将是沉重的处理.. – 2012-01-18 11:38:38

+0

是否有任何Win API编码 – 2012-01-18 11:39:04

回答

4

我不知道如何在WinForms中执行此操作,而无需在事件中添加一些代码。 TextBox的CharacterCasing属性允许您强制输入大写或小写的所有字符,但不能执行正确的框。顺便说一句,这是一个单一的代码行做的一个事件:

TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) 

一个更通用处理器在多个文本框,这样做涉及安装了一系列活动,以同一代码:

'Attach multiple events to this handler 
Private Sub MakeProperCase(sender As Object, e As EventArgs) Handles _ 
    TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus 

    'Get the caller of the method and cast it to a generic textbox 
    Dim currentTextBox As TextBox = DirectCast(sender, TextBox) 
    'Set the text of the generic textbox to Proper Case 
    currentTextBox.Text = StrConv(currentTextBox.Text, VbStrConv.ProperCase) 

End Sub 

在ASP.NET中,你可以可以这样做without code;有一个名为text-transform的CSS属性,此属性的其中一个值为capitalize。应用于文本输入元素时,它会使每个单词的第一个字母大写。

0

这种情况下有两个控件:cboDestination和txtUser。当你键入字母时,它会使你想要的。

Private Sub properCase_TextChanged(sender As Object, e As System.EventArgs) _ 
     Handles cboDestination.TextChanged, txtUser.TextChanged 
     Dim n As Integer = sender.SelectionStart 
     sender.Text = StrConv(sender.Text, VbStrConv.ProperCase) 
     sender.SelectionStart = n 
End Sub