Hello i want to the textbox that when I start to type in names it should be like this

Camille Aisha Cordova
How do I do this?

Recommended Answers

All 16 Replies

Try this,
I'm pretty sure this is how you convert the first letter in each work to upper case.
or you can try useing toupper.

Dim text1 As String = "camille aisha cordova"
Dim text2 As String = StrConv(Text1, VbStrConv.ProperCase)
textbox.text = text2

There is a vb string method you can use when you are done with the textbox

Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave

    Dim str As String = StrConv(txtName.Text, VbStrConv.ProperCase)
    txtName.Text = str

  End Sub

If you want it to Capitolize your string as you type in the textbox you need to Handle the Keypress,KeyUp events of the textbox.

And your point is? Or are you telling me while I was posting to you someone else got there before me? If that is the case; Sorry about that, and have a nice day!

can anyone help me. i decided that i'll just let the user type however way the user wants. just that when the data saves it should be in Sentence Case regardless of how the user typed it.

Dim str As String = StrConv(txtName.Text, VbStrConv.ProperCase)
txtName.Text = str

Use this while saving ur data as mentioned by Phasma... Whats the problem in it?

thank you now it's working, i don't know what happened when i tried it first.
another question, i have too many textboxes to do the sentence, not just only one. what do i do?

Create a function:

Private Function FormatString(Byval txt as string) as String
   Return StrConv(txt, VbStrConv.ProperCase)
End Function

And on saving the data you do for each string you need to format:
Instead of txtName.Text you use FormatString(txtName.Text)

I wouldn't do it on every keypress or leaving the control.

how do i call it?
Call FormatString(txt:=Text1.Text)?

oh i didn't see FormatString(txtName.Text)
sorry sorry :3

this is for FormatString(txtsurname.Text) one textbox only,
what if i want to use it in multiple textboxes?

That is depends on your layout. If you have those textboxes in a groupbox, you could loop the groupbox for all textboxes. Else you have to loop the form and its containers.

Also it is depends on the amount of textboxes you need to format if its worth doingthe loop.

How do you save your stuff? Any code?

strsql = "insert into student_info(Surname, First_Name, Middle_Name, Home_Address, Religion, Tel_No, Name_incase_Emergency, Address_emergency, Tel_No_Emergency, Grade_yearlevel, Place_of_Birth, SchoolLastAttended, Address_School, School_Year, Father_Name, Father_EducAttain, Occupation, EmployedAt, Address_employment,Father_TelNum_Work, Mother_Name, Mother_EducAttain, Occupation_Mother, EmployedAt_Mother, AddEmp_Mother, Mother_TelNum_Work,Birth_Date, Citizenship, General_ave,Gender,Requirements_NSO,Requirements_GoodMoral,Requirements_ReportCard) values ('" & txtsurname.Text & "', '" & txtfirstname.Text & "', '" & txtmiddlename.Text & "', '" & txtaddress.Text & "', '" & txtreligion.Text & "', '" & txtcontactnum.Text & "', '" & txtnameemergency.Text & "', '" & txtaddemergency.Text & "','" & txtcontactemergency.Text & "', '" & gradeyear.Text & "', '" & txtplacebirth.Text & "', '" & txtschoolname.Text & "', '" & txtschooladd.Text & "', '" & txtschoolyear.Text & "', '" & txtfathername.Text & "', '" & txtfathereduattain.Text & "', '" & txtfatheroccupation.Text & "', '" & txtfatheremployedat.Text & "', '" & txtfatheraddemp.Text & "', '" & txtfathertelnum.Text & "', '" & txtmothername.Text & "', '" & txtmothereduattain.Text & "', '" & txtmotheroccupation.Text & "', '" & txtmotheremployedat.Text & "', '" & txtmotheraddemp.Text & "', '" & txtmothertelnum.Text & "', '" & datebirth.Text & "', '" & txtcitizenship.Text & "', '" & txtgenaverage.Text & "', '" & txtgender.Text & "', '" & requirements_nso.Text & "', '" & requirements_goodmoral.Text & "', '" & requirements_reportcard.Text & "')"

        Dim sqlcmd As New SqlClient.SqlCommand
        sqlcmd.CommandText = strsql
        sqlcmd.Connection = sqlconn
        sqlcmd.ExecuteNonQuery()

this is how i save my data, i have way too many textboxes.

ok...
Add the following code to your form class:

Private Sub RecurseContainers(container As Control)
		For Each ctrl In container.Controls
			If TypeOf (ctrl) Is TextBox Then
				ConvertText(CType(ctrl, TextBox))
				Continue For
			End If
			If TypeOf (ctrl) Is Control Then 'panel, groupBox or any other container
				RecurseContainers(CType(ctrl, Control))
			End If
		Next
	End Sub

	Private Sub ConvertText(txt As TextBox)
		txt.Text = FormatString(txt.Text)
	End Sub

	Private Function FormatString(ByVal txt As String) As String
		Return StrConv(txt, VbStrConv.ProperCase)
	End Function

Now add the following line just before you do your "strsql = "insert...." stuff

RecurseContainers(Me)

This will transform all your textboxes.

YES! It works! THANK YOU SO MUCH! :D

I know that I marked this as SOLVED, but I just have a tiny question,
how come this code won't allow me to select all the textbox i just type by using Ctrl + A, or Ctrl + C, or Ctrl + V

If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
'Allowed space and backspace
If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) And e.KeyChar <> ControlChars.Back Then
e.Handled = True
MsgBox("Numbers and special characters is not allowed. Please enter letters")
End If
End If

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.