See comments inline...
private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = true;
((TextBox)sender).Text += e.KeyChar.ToString().ToUpper();
}
The event handler code above isn't sufficient because you don't know that
when the user pressed the key that the cursor is at the end of the textbox.
You'll need to use the SelectionStart and SelectionLength properties to
ensure that your character is placed in the correct location (and whatever
text was selected is removed), e.g.
public static void uCaseReturn(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionStart;
// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substring(0, charPosition) +
Char.ToUpper(e.KeyChar) +
tb.Text.Substring(tb.SelectionStart + tb.SelectionLength);
// Place the cursor in the correct location
tb.SelectionLength = 0;
tb.SelectionStart = charPosition;
// Mark the keystroke as being handled
e.Handled = true;
}
}
Ken