i am making a project in vb.net and i have a bio data form in it
i have 2 textbox in it, TextBox1 is for Name and the Textbox2 is for father's name . i want to make the first letters of Name in textBox1 as Capital(eg: Pratik Shukla) in both the TextBox .on focus lost

Dani AI

Generated

Good asks about doing this on focus lost. Building on ’s manual casing and ’s pointer, you can lean on .NET’s culture-aware title casing so you do not have to juggle Mid/UCase yourself. It handles hyphenated and apostrophized names well and respects the current culture. Note that it is not linguistically perfect and acronyms may need special handling; see Microsoft’s notes on TextInfo.ToTitleCase for the caveats. Change case in .NET. (learn.microsoft.com)

If this is WinForms, wire the formatting to the Leave event so it runs exactly when the textbox loses focus (avoids the caret jump that @Ge48’s TextChanged approach can cause while typing):

Imports System.Globalization
Imports System.Text.RegularExpressions

Private Function TitleCaseName(value As String) As String
    If String.IsNullOrWhiteSpace(value) Then Return String.Empty
    Dim ti = CultureInfo.CurrentCulture.TextInfo
    Dim s = Regex.Replace(value.Trim(), "\s+", " ")           'collapse extra spaces
    s = ti.ToTitleCase(s.ToLower(CultureInfo.CurrentCulture)) 'normalize, then title case
    s = Regex.Replace(s, "\b(JR|SR|II|III|IV)\b",
                      Function(m) m.Value.ToUpper(), RegexOptions.IgnoreCase) 'optional
    Return s
End Function

Private Sub Name_Leave(sender As Object, e As EventArgs) _
    Handles TextBox1.Leave, TextBox2.Leave
    Dim tb = DirectCast(sender, TextBox)
    tb.Text = TitleCaseName(tb.Text)
End Sub

For ASP.NET Web Forms, you can do the same formatting in the TextChanged handler with AutoPostBack="true" if you prefer server-side, or attach a small onblur script to format client-side and avoid a postback. Either way, the core casing logic above applies. The ToTitleCase behavior (acronyms remain uppercase; operation is culture-sensitive) is documented here. Change case in .NET. (learn.microsoft.com)

Recommended Answers

All 3 Replies

OK, you will need to use the UCase, and mid functions simply like this:

Function GetFormattedName AS String
dim FormattedName as string =""
dim FirstName as String =""
dim LastName as String =""

if trim(Textbox1.text) <> "" then
    Firstname  = trim(Textbox1.text)
end if
if trim(Textbox2.text) <> "" then
    Lastname  = trim(Textbox2.text)
end if

If Firstname <>"" then
    'Fully formatted Name   Uppercase First char    Lowercase the rest and add a space
    FormattedName = UCASE(mid(FirstName,1,1)) & lcase(mid(FirstName,2)) &" "
end if

If LastName <> "" then
    'Append last name in the correct format as above
    FormattedName = FormattedName  & UCASE(mid(LastName,1,1)) &lcase(mid(LastName,2))
End If

GetFormattedName = trim(FormattedName) 
'If we did not get any values we will get an empty string
'If we only got FirstName, the trim will strip out the trailing space.
End Function

You could use the TextChanged property of the textboxes. This code will change all first letters to upper case (ProperCase) as soon as text is put into your textboxes. Instead of ProperCase, UpperCase or Lowercase can also be used.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        If TextBox1.Text <> "" Then
            Dim a As String = TextBox1.Text
            TextBox1.Text = (StrConv(a, VbStrConv.ProperCase))
            TextBox1.Select(TextBox1.Text.Length, 0)
        End If
        If TextBox2.Text <> "" Then
            Dim a As String = TextBox2.Text
            TextBox2.Text = (StrConv(a, VbStrConv.ProperCase))
            TextBox2.Select(TextBox2.Text.Length, 0)
        End If
    End Sub
commented: +1 for the code +2
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.