Hi,

I try to change all first letter of the words in the textbox to uppercase.
THis how far i try to change it but just the first word not the entire words.

Private Sub txtQuote_Change()
  txtQuote.Text = UCase$(Left(txtQuote.Text, 1)) & LCase$(Mid$(txtQuote.Text, 2))
  txtQuote.SelStart = Len(txtQuote.Text)
End Sub

Please Help.
Best regards

Thank you.

Dani AI

Generated

The reason the original routine only capitalized the first word is that it only uppercased the very first character of the whole TextBox and lowercased the rest. For classic VB/VBA the simplest built-in fix is what suggested (StrConv with vbProperCase). In VB.NET use the culture-aware TextInfo.ToTitleCase API for the equivalent behavior.

Do the conversion on Leave/LostFocus (or Validate) rather than on every keystroke to avoid fighting the caret and the undo buffer. If you must change text while the user types, save and restore the caret/selection around the replacement so the cursor does not jump. Also avoid always forcing the caret to the end — preserve SelectionStart/SelectionLength so typing feels natural.

Example (VB.NET — run on Leave to minimize disruption):

Private Sub txtQuote_Leave(sender As Object, e As EventArgs) Handles txtQuote.Leave
    Dim startPos As Integer = txtQuote.SelectionStart
    Dim selLen As Integer = txtQuote.SelectionLength

    Dim ci = System.Threading.Thread.CurrentThread.CurrentCulture
    Dim newText = ci.TextInfo.ToTitleCase(txtQuote.Text.ToLowerInvariant())

    If newText <> txtQuote.Text Then
        txtQuote.Text = newText
        txtQuote.SelectionStart = Math.Min(startPos, txtQuote.Text.Length)
        txtQuote.SelectionLength = 0
    End If
End Sub

Caveats: title-casing routines are culture-sensitive and can mangle acronyms (SQL -> Sql) or special names (McDonald, O'Neill), and they may treat punctuation as word breaks. If you need precise control, apply post-processing rules or a small exception dictionary (handle all-uppercase words, known acronyms, and family-name patterns). Thanks to for the StrConv pointer and to for confirming the thread was resolved.

Recommended Answers

All 2 Replies

Why not use StrConv()

See if this helps :

Private Sub txtQuote_Change()
  txtQuote.Text = StrConv(txtQuote.Text, vbProperCase)
  txtQuote.SelStart = Len(txtQuote.Text)
End Sub
commented: Very Quckly Response +2
commented: :D +2
commented: Agree +3

Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.