How to move the textbox caret to the right

15,823

Solution 1

Set the CharacterCasing property of the TextBox to Upper; then you don't need to process it manually.

Note that textBox3.Text += e.KeyChar.ToString().ToUpper(); will append the new character to the end of the string even if the input caret is in the middle of the string (which most users will find highly confusing). For the same reason we cannot assume that the input caret should appear at the end of the string after inputting the character.

If you would still really want to do this in code, something like this should work:

// needed for backspace and such to work
if (char.IsControl(e.KeyChar)) 
{
    return;
}
int selStart = textBox3.SelectionStart;
string before = textBox3.Text.Substring(0, selStart);
string after = textBox3.Text.Substring(before.Length);
textBox3.Text = string.Concat(before, e.KeyChar.ToString().ToUpper(), after);
textBox3.SelectionStart = before.Length + 1;
e.Handled = true;

Solution 2

            tbNumber.SelectionStart = tbNumber.Text.ToCharArray().Length;
            tbNumber.SelectionLength = 0;

Solution 3

private void txtID_TextChanged(object sender, EventArgs e)
{
    txtID.Text = txtID.Text.ToUpper();
    txtID.SelectionStart = txtID.Text.Length;
}

Solution 4

This will preserve the location of the insertion point (but persionally I'd go with the answer given by Fredrik Mörk)

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)    
{        
    int selStart = textBox3.SelectionStart;
    textBox3.Text += e.KeyChar.ToString().ToUpper();        
    textBox3.SelectionStart = selStart;
    e.Handled = true;  
}

SelectionStart might actually be called SelStart, I don't have a compiler handy at the moment.

Solution 5

If you have to do this manually, you can use

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
    textBox3.Text += e.KeyChar.ToString().ToUpper();
    textBox3.SelectionStart = textBox3.Text.Length;
    e.Handled = true;
}

But the preceding code inserts the new character at the end of the text. If you want to insert it at where the cursor is:

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
    int selStart = textBox3.SelectionStart;
    textBox3.Text = textBox3.Text.Insert(selStart,e.KeyChar.ToString().ToUpper());
    textBox3.SelectionStart = selStart + 1;
    e.Handled = true;
}

This code inserts the new character at the cursor position and moves the cursor to the left of the newly inserted character.

But i still think setting CharacterCasing is better.

Share:
15,823
monkey_boys
Author by

monkey_boys

Nothing I' am alone boy and be live for my dream Nothing about me Csharp Snippet Piyanut Khajohnsubdee King Mongkut's University of Technology North Bangkok Google's Profile programmatically : Dynamic Style ปิยะณัฐ ขจรทรัพย์ดี

Updated on July 26, 2022

Comments

  • monkey_boys
    monkey_boys almost 2 years

    I would like to change all the characters entered into a textbox to upper case. The code will add the character, but how do I move the caret to the right?

    private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
    {
        textBox3.Text += e.KeyChar.ToString().ToUpper();
        e.Handled = true;
    }
    
  • Rabeel
    Rabeel almost 15 years
    +1: I provided an alternative answer as it can be useful in other situations.
  • Todd Moses
    Todd Moses over 12 years
    This is a great way to add text before the user types.