# VBForums CodeBank > CodeBank - Visual Basic .NET >  Uppercase first letter in each word of a string.

## noahssite

Here is very simple code to uppercase the first letter of each word in a string.

What i have done in this code is stored all the words into an array. Then by looping through the array the word is being split in two, the first part of the substring is the first letter. The first letter is then capitalized. After that is done the word is joined back together to make a whole. This process is done for every word in the array. As the process takes action the new edited word replaces the old word in the same array. The words from the array are being placed on a single line which will become the new value of Textbox1. 


vb Code:
'Try something like so:
        Dim words() As String
        words = TextBox1.Text.Split(" "c)
        For x As Integer = 0 To words.Rank + 1
            words(x) = (words(x).Substring(0, 1).ToUpper & words(x).Substring(1, words(x).Length - 1))
        Next
        Dim NewString As String = String.Empty
        For i As Integer = 0 To words.Rank + 1
            If i = 0 Then
                NewString = (words(i))
            Else
                NewString = (NewString & " " & words(i))
            End If
        Next
        TextBox1.Text = NewString


EDIT: Here is an even simpilar code that will produce the same result as my code above.

vb Code:
MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)

----------


## stru4nak

Good code but see this

vb Code:
Dim s As String = "the quick brown fox jumps over the lazy dog"Dim s2 As String = StrConv(s, VbStrConv.ProperCase)MessageBox.Show(s2)

----------


## noahssite

Now that just makes my code look like a waste of CPU Usage...
The first reason i got it out of my signature.

----------


## stru4nak

Sry man  :Big Grin:

----------


## Deepak Sakpal

I also have a post in CodeBank for the same topic here.

----------


## chathu1234

Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?

----------


## noahssite

> Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?


In the Textbox's keyup event place this code, assuming your textbox is TextBox1:



```
TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
```

----------


## chathu1234

Thanks Noah but it will only allow to type backwards.example: if you type "upper case" it will give you "Esa Creppu"
How can I fix this

----------


## noahssite

You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.

e.g.



```
TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
TextBox1.SelectionStart = TextBox1.Length - 1
```

What is happening is that when the Textbox's value is changed the Selection of the Textbox is being reset to 0.

----------


## chathu1234

Thank you Noah for taking your time to help me.But your code did not resolve the problem.Now when I enter the first letter the cursor jumps back to the beginning of the text.from then on in it works fine.
Ex: Input - upper case
     Output - Pper Caseu
Can you help me please?
Im using VB 2008 and vb2010.Both do not support textbox.length
Had to use textbox.TextLength

Thank you so much

----------


## chathu1234

> You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.
> 
> e.g.
> 
> 
> 
> ```
> TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
> TextBox1.SelectionStart = TextBox1.Length - 1
> ...


I figured it out Noah.
Instead of 

```
TextBox1.SelectionStart = TextBox1.Length - 1
```

I used 

```
TextBox1.SelectionStart = TextBox1.TextLength + 1
```

It solved the issue.Thank you so much

----------


## noahssite

Oh my mistake.

This should have worked, I believe:



```
TextBox1.SelectionStart = TextBox1.Length
```

with no adding/subtracting.

----------


## mikrodotnet

> vb Code:
> MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)


thanks!

----------


## nolocimes

> Good code but see this
> 
> vb Code:
> Dim s As String = "the quick brown fox jumps over the lazy dog"
> Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
> MessageBox.Show(s2)


Thank you very much sir

----------

