2012-01-18 72 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