Rastapopoulos
Rastapopoulos

Reputation: 9

How to force text being entered into a textbox into Uppercase?

I want to make the text that a user is typing into a textbox become uppercase. I know two ways to do this, which are:

Textbox1.Text = UCase(Textbox1.Text)
or
Textbox1.Text = Textbox1.Text.ToUpper

HOWEVER: both have the same problem (when embedded in a Textbox1_TextChanged event handler), which is that the cursor keeps being moved back to the start, so if you slowly type in, say abcdef, it comes out as FEDCBA. Is there a way to move the cursor back to the end of the string after each time it works to make the text uppercase?

Upvotes: 0

Views: 15876

Answers (6)

Rastapopoulos
Rastapopoulos

Reputation: 9

Your version didn't quite work for me in Visual Basic 2019, but it formed the basis of this, which does (where "txtPrem1" is the TextBox):

Private Sub txtPrem1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrem1.KeyPress
        Dim KeyAscii = AscW(e.KeyChar)
        If KeyAscii > 96 And KeyAscii < 123 Then
            'Typed letter from "a-z", map it to "A-Z"
            KeyAscii = KeyAscii - 32
        End If
        e.KeyChar = ChrW(KeyAscii)
    End Sub

Upvotes: -1

Analise This
Analise This

Reputation: 11

Why don't you just place the code Textbox1.Text = Textbox1.Text.ToUpper in the Textbox1_LostFocus event instead of the Textbox1_TextChanged event. It's so simple and it works even if you paste text into the field. As soon as your cursor moves to another field, the event is triggered, causing the text to change case from lower to upper.

Upvotes: 1

sam_v
sam_v

Reputation: 513

You could also use:

Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress
    e.KeyChar = UCase(e.KeyChar)
End Sub

... if you need to do some custom formatting or logic. Otherwise I'd also suggest using the default TextBox property CharacterCasing set to Upper.

Please note that this solution does not handle the situation when user is pasting text into the TextBox component and you have to programmatically take care of that situation too if needed, but the TextBox property CharacterCasing does it for you even if user is pasting text into the component.

Upvotes: 0

Chris Raisin
Chris Raisin

Reputation: 442

How about this:

Private Sub MyText_TextChanged(sender As Object, e As EventArgs) _
                              Handles MyText.TextChanged
    Dim oText As TextBox = CType(sender, TextBox)
    oText.Text = oText.Text.ToUpper
    oText.SelectionStart = oText.Text.Length
    oText.SelectionLength = 0
End Sub

Upvotes: -1

thatOneDude
thatOneDude

Reputation: 119

go to textbox property, change CharacterCasing to Upper

enter image description here

Upvotes: 9

braX
braX

Reputation: 11755

Use the KeyPress event to detect lower case letters being entered, and convert them to uppercase as you go:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If KeyAscii > 96 And KeyAscii < 123 Then
    'Typed letter from "a-z", map it to "A-Z"
    KeyAscii = KeyAscii - 32
  End If
End Sub

Ucase() is used only after the person is done entering the text.

If you are using VB.NET then you just need to set the .CharacterCasing property of the TextBox to .Upper - No code needed. But if you wanted to use code for some reason, use this:

TextBox1.CharacterCasing = CharacterCasing.Upper

Upvotes: 2

Related Questions