Re: Event to return value
From: Ken Kolda (ken.kolda_at_elliemae-nospamplease.com)
Date: 07/09/04
- Next message: james: "Re: Boolean Naming Conventions"
- Previous message: Steve: "RE: Passing info from one from to another"
- In reply to: William Ryan eMVP: "Re: Event to return value"
- Next in thread: Steve: "Re: Event to return value"
- Reply: Steve: "Re: Event to return value"
- Reply: William Ryan eMVP: "Re: Event to return value"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 9 Jul 2004 10:51:42 -0700
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
- Next message: james: "Re: Boolean Naming Conventions"
- Previous message: Steve: "RE: Passing info from one from to another"
- In reply to: William Ryan eMVP: "Re: Event to return value"
- Next in thread: Steve: "Re: Event to return value"
- Reply: Steve: "Re: Event to return value"
- Reply: William Ryan eMVP: "Re: Event to return value"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|