views:

1446

answers:

6

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;
  }
+8  A: 

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;
Fredrik Mörk
+1: I provided an alternative answer as it can be useful in other situations.
Binary Worrier
A: 

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.

Binary Worrier
A: 

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.

erelender
A: 

If for some reason you still want to do it in the event:

textBox3.SelectionStart = textBox3.Text.Length;
CD
A: 

Another method is to just change the value of the KeyChar itself:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
        if ((int)e.KeyChar >= 97 && (int)e.KeyChar <= 122) {
            e.KeyChar = (char)((int)e.KeyChar & 0xDF);
        }
    }

Although, using the CharacterCasing property is the easiest solution.

Chris Dunaway
A: 
            tbNumber.SelectionStart = tbNumber.Text.ToCharArray().Length;
            tbNumber.SelectionLength = 0;
jdc