How can I make the first letter of every word to uppercase, In Visual Basic

Gary Simpson 471 Reputation points
2020-11-30T22:41:38.37+00:00

Hi Great People

I am trying to make the first letter of each word in a textbox to Uppercase Maybe using Keypress Event, or Keydown event.

Lets say a user types in textbox1 (mickey m mouse) the result I require is (Mickey M Mouse) also there could be one just one word up to 5 words.

I have search the forum to no avail, Can any of you great people help?

Kind Regards

Gary

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-11-30T23:22:38.577+00:00

    Try this:

       Private Sub TxtGender_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
         With TextBox1
         Dim ss As Integer = .SelectionStart
         Dim sl As Integer = .SelectionLength
         .Text = StrConv(.Text, VbStrConv.ProperCase)
         .SelectionStart = ss
         .SelectionLength = sl
         End With
         End Sub
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-12-01T00:06:15.653+00:00

    Hi
    Here is one way - this deals with 3 TextBoxes (1,2 and3), but any number could be added.

        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
            Dim tb As TextBox = DirectCast(sender, TextBox)
            MakeProper(tb)
        End Sub
        Sub MakeProper(tb As TextBox)
            With tb
                Dim ss As Integer = .SelectionStart
                Dim sl As Integer = .SelectionLength
                .Text = StrConv(.Text, VbStrConv.ProperCase)
                .SelectionStart = ss
                .SelectionLength = sl
            End With
        End Sub
    
    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,366 Reputation points
    2020-12-01T13:23:13.523+00:00

    Hello @Gary Simpson

    Even though you have a solution you might also consider using a custom TextBox. There are two properties shown below to convert .Text to title case. The OverrodeUpperCased needs to be set if you have something like KAREN PAYNE while not for karen payne.

    Source code

    Imports System.ComponentModel  
    Imports System.Globalization  
      
    Namespace Controls  
        ''' <summary>  
        ''' * Suppress beep on ENTER key pressed  
        ''' * Provides ability to title case text  
        ''' </summary>  
        Public Class NoBeepTitleCaseTextBox  
            Inherits TextBox  
      
            Public Delegate Sub TriggerDelegate()  
            ''' <summary>  
            ''' Subscribe to be notified when ENTER was pressed.  
            ''' </summary>  
            Public Event TriggerEvent As TriggerDelegate  
            Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)  
                If e.KeyCode = Keys.Enter Then  
      
                    e.Handled = True  
                    e.SuppressKeyPress = True  
      
                    If ToTitleCase Then  
                        HandleCasing()  
                    End If  
      
      
                    TriggerEventEvent?.Invoke()  
      
                    Return  
      
                End If  
      
                MyBase.OnKeyDown(e)  
            End Sub  
            ''' <summary>  
            ''' Use to transform .Text to title case.  
            ''' </summary>  
            Private Sub HandleCasing()  
      
                If useTitleCase Then  
                    Dim textInfo As TextInfo = (New CultureInfo("en-US", False)).TextInfo  
      
                    If uppercaseOption Then  
                        Text = Text.ToLower()  
                    End If  
      
                    Text = textInfo.ToTitleCase(Text)  
                    SelectionStart = Text.Length  
                    SelectionLength = 0  
                End If  
      
            End Sub  
      
            Private useTitleCase As Boolean  
            Private uppercaseOption As Boolean  
            <Browsable(True)>  
            <Category("Extended Properties")>  
            <Description("Set text to title case")>  
            <DisplayName("ToTitleCase")>  
            Public Property ToTitleCase() As Boolean  
                Get  
                    Return useTitleCase  
                End Get  
                Set  
                    useTitleCase = Value  
                End Set  
            End Property  
            <Browsable(True)>  
            <Category("Extended Properties")>  
            <Description("Override casing when all text may be uppercased")>  
            <DisplayName("OverrideUpperCased")>  
            Public Property OverrideUpperCased() As Boolean  
                Get  
                    Return uppercaseOption  
                End Get  
                Set  
                    uppercaseOption = Value  
                End Set  
            End Property  
            ''' <summary>  
            ''' We can not trap TAB in OnKeyDown so handle it here  
            ''' then permit default via MyBase.ProcessCmdKey  
            ''' </summary>  
            ''' <param name="msg"></param>  
            ''' <param name="keyData"></param>  
            ''' <returns></returns>  
            Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean  
                If keyData = Keys.Tab Then  
                    If ToTitleCase Then  
                        HandleCasing()  
                    End If  
                End If  
      
                Return MyBase.ProcessCmdKey(msg, keyData)  
      
            End Function  
        End Class  
    End Namespace  
    

    Test code

    Public Class MainForm  
        Private Sub UseTitleCaseCheckBox_CheckedChanged(sender As Object, e As EventArgs) _  
            Handles UseTitleCaseCheckBox.CheckedChanged  
      
            NameTextBox1.ToTitleCase = UseTitleCaseCheckBox.Checked  
            NameTextBox2.ToTitleCase = UseTitleCaseCheckBox.Checked  
      
        End Sub  
      
        Private Sub OverrideCasingCheckBox_CheckedChanged(sender As Object, e As EventArgs) _  
            Handles OverrideCasingCheckBox.CheckedChanged  
      
            NameTextBox1.OverrideUpperCased = OverrideCasingCheckBox.Checked  
            NameTextBox2.OverrideUpperCased = OverrideCasingCheckBox.Checked  
      
        End Sub  
    End Class  
    

    44075-ep1.png

    44142-ep.png

    1 person found this answer helpful.
    0 comments No comments

  3. Anonymous
    2020-11-30T23:00:07.257+00:00
        Dim s As String = "mickey m mouse)"
        Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
        '  s2 = "Mickey M Mouse"