none
Force Uppercase

    Question

  • I have a textbox into which data is keyed.
    I want to be able to have the keys entered set to upper case before they appear in the textbox.

    I dont want the user to have to use the shift key. how can i do this?

    Can i do something inside the KeyDown or KeyPressed event that will convert them to uppercase
    Friday, October 21, 2005 1:37 PM

Answers

  • Found a solution

    Add the following code into the textbox's keyup event


    txtBTAddr1.Text = txtBTAddr1.Text.ToUpper
    txtBTAddr1.Select(txtBTAddr1.Text.Length, 0)

     

    Friday, October 21, 2005 2:26 PM

All replies

  • Add this code to the KeyPress event:


    if (char.IsLower(e.KeyChar))
    {
        e.KeyChar =
    char
    .ToUpper(e.KeyChar);
    }

     
    • Proposed as answer by Morphlin Tuesday, October 16, 2012 5:19 PM
    Friday, October 21, 2005 2:03 PM
  • I have tried this, however it says the KeyChar is ReadOnly



    Private Sub txtBTAddr1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBTAddr1.KeyPress
            If Char.IsLower(e.KeyChar) Then
                e.KeyChar = Char.ToUpper(e.KeyChar)
            End If
        End Sub

     
    Friday, October 21, 2005 2:09 PM
  • I've tried it on a regular WinForms app, in VS2005.

    I don't know, it might be a CF or a version problem... Sad
    Friday, October 21, 2005 2:14 PM
  • Found a solution

    Add the following code into the textbox's keyup event


    txtBTAddr1.Text = txtBTAddr1.Text.ToUpper
    txtBTAddr1.Select(txtBTAddr1.Text.Length, 0)

     

    Friday, October 21, 2005 2:26 PM
  • Not a good solution...

    You're converting all the text to upper case. This makes caracter insertion impossible. The cursor won't move....

    Keychar shound't be read-only!!!!

    Wednesday, August 30, 2006 12:36 AM
  • hello Barry,

    Maybe you could use the OpenNETCF.Windows.Forms.TextBox2 control.
    It has a property that configures the character casing that should be used:

    textBox.CharacterCasing = CharacterCasing.Upper;

    Greetings,

    Peter Vrenken



    • Proposed as answer by Ash28 Monday, September 10, 2012 12:26 PM
    Thursday, August 31, 2006 12:09 PM
  • or you could just make whatever the user types in, to uppercase after they press a continue button or something.

    Dim theString as string = Me.theTextBox.Text.ToUpper()

    Thursday, August 31, 2006 12:22 PM
  • Add the below code in a css file and add it to your project. Apply this css to the control say a textbox like :
    <asp:TextBox ID="txtSurname" CssClass="TextBoxUpperCase" runat="server" ><asp:TextBox>


    .TextBoxUpperCase
    {
    text-transform: uppercase;
    color: black;
    }

    This will convert the case to upper as you type in the control.

    Cheers!

    Friday, March 02, 2007 3:10 PM
  • e.KeyChar is readonly as passed from the base control.  The following works in VB.NET:

     

    If Char.IsLower(e.KeyChar) Then

        e.Handled = True

        SendKeys.Send(Char.ToUpper(e.KeyChar))

    End If

    Wednesday, May 30, 2007 3:02 PM
  • This works sucessfuly..

    Rupesh Kumar
    Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers MedalsFriday, March 02, 2007 3:10 PM
    Add the below code in a css file and add it to your project. Apply this css to the control say a textbox like :
    <asp:TextBox ID="txtSurname" CssClass="TextBoxUpperCase" runat="server" ><asp:TextBox>

     


    .TextBoxUpperCase
    {
    text-transform: uppercase;
    color: black;
    }

    This will convert the case to upper as you type in the control.

     

    Cheers!



    thks lot!!
    Wednesday, May 20, 2009 2:52 PM
  • Thank for  valuable information..

    great man

    • Proposed as answer by muneer uc Friday, September 16, 2011 4:52 AM
    Friday, September 16, 2011 4:52 AM
  • I think this is the easiest way: 

    Title Make a TextBox convert letters to upper case in VB.NET
    Description This example shows how to make a TextBox convert letters to upper case in VB.NET by setting the control's CharacterCasing property.
    Keywords TextBox, lower case, VB.NET
    Categories Controls, VB.NET
     
    When the form loads, it sets the TextBox's CharacterCasing property to Upper.
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Load
        ' Set the TextBox's character casing.
     TextBox2.CharacterCasing = CharacterCasing.Upper
    End Sub

    http://www.vb-helper.com/howto_net_uppercase.html

    Friday, June 08, 2012 10:22 PM
  • I know this thread is out dated but I am writing this just to help if anyone struck up with the same issue.

    Yes,We can handle it in KeyPressEvent.I was facing same issue and it fixed it as below.

    1. Created a Custom contol and overridden the keypress event.

    Below is the code snippet.

    public partial class myTextBox : TextBox

    {

    public myTextBox ()

    {

    InitializeComponent();

    }

    protected override void OnKeyPress(KeyPressEventArgs e)

    {

    int start = SelectionStart;

    if (SelectionLength > 0)

    SelectedText =

    string.Empty;

    if (Text.Length == MaxLength)

    {

    e.Handled =

    true;

    }

    else

    {

    if (char.IsLower(e.KeyChar))

    {

    Text = Text.Insert(start,

    char.ToUpper(e.KeyChar).ToString());

    SelectionStart = start + 1;

    e.Handled =

    true;

    }

    base.OnKeyPress(e);

    }

    }

    }

    Monday, January 07, 2013 9:43 AM