Re: Event to return value

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: William Ryan eMVP (dotnetguru_at_comcast.nospam.net)
Date: 07/09/04


Date: Fri, 9 Jul 2004 17:14:39 -0400

If you need to have everything capitalized then setting the CharacterCasing
is the way to go . I agree with you though about the positioning.

-- 
W.G. Ryan, eMVP
Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
"Ken Kolda" <ken.kolda@elliemae-nospamplease.com> wrote in message
news:%237eQP2dZEHA.4032@TK2MSFTNGP11.phx.gbl...
> 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
>
>


Relevant Pages

  • Re: Caret Position in Textbox Control
    ... The position of the caret is always at SelectionStart. ... the caret in the character array in the TextBox. ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Event to return value
    ... 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 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Event to return value
    ... Wouldn't it be easier to do this on the TextChanged event? ... > You'll need to use the SelectionStart and SelectionLength properties to ... > ensure that your character is placed in the correct location (and whatever ...
    (microsoft.public.dotnet.languages.csharp)
  • Slow RichTextBox property
    ... I was using the RichTextBox property called SelectionStart to move ... character is different from the next. ... very slow and takes alot of processing time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Converting a string to an integer
    ... interpreted what a 'white space character' is.. ... public static void main{ ... The '*' character can import into the namespace, ... I usually use package imports in SSCCE's, ...
    (comp.lang.java.programmer)