See comments inline...
private void textBox1_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
e.Handled = true;
((TextBox)sende r).Text += e.KeyChar.ToStr ing().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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
if (!Char.IsContro l(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionSta rt;
// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substri ng(0, charPosition) +
Char.ToUpper(e. KeyChar) +
tb.Text.Substri ng(tb.Selection Start + tb.SelectionLen gth);
// Place the cursor in the correct location
tb.SelectionLen gth = 0;
tb.SelectionSta rt = charPosition;
// Mark the keystroke as being handled
e.Handled = true;
}
}
Ken