简体   繁体   中英

How to convert string to uppercase in windows textbox?

I've a textbox in my windows application. It allows only alphabets and digits. I want when ever I type any alphabet, it should be converted to uppercase.How can I do that and in which event? I've used str.ToUpper() but the cursor is shifting to the beginning of the string. Please give me solution.

您只需将CharacterChasing属性更改为Upper

textBox1.CharacterCasing = CharacterCasing.Upper

Why to reinvent the wheel, just set 'CharacterCasing' property of textBox to 'Upper'. You don't need to write any code.

使用textBox大写字母

In case of masked textbox, you can use '>' (in mask property) to make following characters uppercase. eg For a input alphanumeric string (AZ, 0-9) of length eight, use mask '>AAAAAAAA'. To restrict to letters only (AZ), use '>LLLLLLLL'.

使用maskedTextBox大写字母

You need to assign the results of ToUpper back to the textbox:

txtBox.Text = txtBox.Text.ToUpper();

Alternatively, set the CharacterCasing property of the textbox to Upper :

txtBox.CharacterCasing = CharacterCasing.Upper;

Try to use KeyPress event and the handler should be similar to the following;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar= (e.KeyChar.ToString()).ToUpper().ToCharArray()[0];
        }

In properties of TextBox simply set CharacterCasing to Upper. It'll convert all entered character in uppercase.

Maybe you should use the event: TextBox1_EditValueChanging.

If each time the cursor moves at the first position, you can calculate the number of characters in your txt and shift the cursor after the last charachter.

private void mytextbox_KeyPress(object sender, KeyPressEventArgs e)

{

e.KeyChar = Char.ToUpper(e.KeyChar);

}
  1. TxtInput //This is the textbox user inputs
  2. LblLengthstrong //Label to show length
  3. LblUpper //Label that shows it in uppercase
  4. LblLower //Label that shows it in lowercase
  5. LblRight //Label that shows shows last 3 characters
  6. LblSubscript //Label that shows characters 1 through 3

//show length of all characters inputed

 private void BtnLength_Click(object sender, EventArgs e)
        {
            LblLength.Text = TxtInput.Text.Length.ToString();
        }

//make to characters upper

  private void btnUpper_Click(object sender, EventArgs e)
        {
            LblUpper.Text = TxtInput.Text.ToUpper();
        }

//make characters to lowercase

 private void BtnLower_Click(object sender, EventArgs e)
        {
            LblLower.Text = TxtInput.Text.ToLower();
        }

//show last 3 characters

 private void BtnRight_Click(object sender, EventArgs e)
        {
            LblRight.Text = TxtInput.Text.Substring(TxtInput.Text.Length - 3);
        }

//show characters in position 1 through 3

 private void BtnSubscript_Click(object sender, EventArgs e)
        {
            LblSubscript.Text = TxtInput.Text.Substring(1, 3);
        }

//ASCII

  private void BtnGo_Click(object sender, EventArgs e)
        {
            string name;
            int letter;

            name = TxtInput.Text;

            for (int index = 0; index < name.Length; index++)
            {
                letter = name[index];
                MessageBox.Show(letter.ToString());
            }
        }

//Password

      int InNumTry = 0;
    private void BtnGo_Click_1(object sender, EventArgs e)
    {
        string password;
        password = TxtIn.Text;

            switch (password)
            {
                case " ": MessageBox.Show("Passowrd is empty.");
                    break;

                case "MIKE": MessageBox.Show("Password is OK!");
                    FrmBOO newForm = new FrmBOO();
                    newForm.Show();
                    break;

                default:
                    InNumTry++;
                    MessageBox.Show("Invalid Passwrod, try again!");
                    TxtIn.Text = "";
                    TxtIn.Focus();
                    break;
            }

            if (InNumTry >= 3)
            {
                MessageBox.Show("You have tried too many times, have a good day.");
                TxtIn.Enabled = false;
            }
        }

// Adding timer(In the timer add the code under this (Add add timer1.Start(); in the start form)

 private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime datetime = DateTime.Now;
            this.LblTime.Text = datetime.ToString();
        }

右键单击Designer中的TextBox,在Properties下将CharacterCasing更改为Upper。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM