How to make the characters uppercase when you enter text in the TextBox by using Visual Basic .NET or Visual Basic 2005

Summary

This step-by-step article describes how to make the characters uppercase when you enter text in the TextBox by using Microsoft Visual Basic .NET or Microsoft Visual Basic 2005. The Visual Basic .NET or Visual Basic 2005 examples in this article describe how to use the CharacterCasing property in the designer to make the characters uppercase, and how to create custom code by using the KeyPress event to make the characters uppercase.

back to the top

Make the Characters Uppercase by Using the CharacterCasing Property


The TextBox has a CharacterCasing property that allows you to set the case of the characters that you type in the TextBox. By default, CharacterCasing is set to Normal. Therefore, the characters remain unchanged. For example, if you press the SHIFT+A key combination, an uppercase "A" is inserted in the TextBox. To make the characters uppercase by using the CharacterCasing property, follow these steps:
  1. Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. Under Project Types, click to select
    Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click to select Windows Application.

    By default, Form1 is created.
  5. From the toolbox, drag a
    TextBox control to Form1.
  6. Right-click TextBox1, and then click
    Properties.
  7. In the Properties window, locate the
    CharacterCasing property, and then click to select
    Upper from the list.
  8. On the Debug menu, click
    Start.
  9. Type text in the TextBox.

    All the characters are uppercase.
back to the top

Make the Characters Uppercase by Using Custom Code

  1. Open Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. Under Project Types, click to select
    Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click to select Windows Application.

    By default, Form1 is created.
  5. From the toolbox, drag a
    TextBox control to Form1.
  6. On the View menu, click
    Code.
  7. Add the following code to the KeyPress event of TextBox1:
    ' Operate only on lowercase characters, so that  Backspace, Delete, Home, and others 
    ' continue to work without interference.

    If Char.IsLower(e.KeyChar) Then

    'Convert to uppercase, and put at the caret position in the TextBox.
    TextBox1.SelectedText = Char.ToUpper(e.KeyChar)

    e.Handled = True
    End If
  8. On the Debug menu, click
    Start.
  9. Type text in the TextBox.

    All the characters are uppercase.
back to the top
Propriedades

ID do Artigo: 818363 - Última Revisão: 26 de nov de 2007 - Revisão: 1

Comentários