The reason I can't do that is because sometimes the keystroke is
allowed and sometimes it is not.
-  If my string is at its max length and I type a key after to make
the string its max length plus one, then that keystroke is disallowed.
-  If my string is at its max length, but my keystroke replaces a
character that is already in the string, keeping the string length the
same, then the keystroke the same length, then the keystroke is
allowed.

The solution you have is what I started with.  The issue is that once
my string is its max length, I don't want to ignore ANY keystroke, I
only want to ignore any keystroke that would make my string longer,
and not all keystrokes make my string longer.

On Sep 26, 10:26 am, Cerebrus <[email protected]> wrote:
> Why not simply use the (Textbox.Text.Length - Textbox.SelectionLength)
> calculation to determine if a Keypress is to be allowed or not ?
>
> For instance,
>
> ---
> const int maxLen = 6;
> int textLen = Textbox1.Text.Length;
> int selLen = Textbox1.SelectionLength;
> if (textLen - selLen + 1 > maxLen)
>   // Suppress KeyPress.
> ---
>
> You may have to refine that logic since I haven't tested it. You
> shouldn't have to tell the difference between a keystroke that
> replaces a selection and one that doesn't.
>
> On Sep 26, 5:50 pm, Tom <[email protected]> wrote:
>
>
>
> > I am using the KeyDown event to edit a text box as the user types.  If
> > the user types a key I don't like I use the
> > KeyEventArgs.SuppressKeyPress property to suppress that key stroke.
>
> > One of the edits is that the maximum length of input is 6 characters.
> > I can have code that says if the TextBox.Text.Length property is 6
> > then suppress further keystrokes.  The problem with that is what if
> > the user typed "1234546", then uses the mouse to highlight the "3" and
> > wants to change it to "9"?  I wouldn't want to suppress that key
> > stroke because it doesn't cause the length to exceed 6 positions.  I
> > know I have the MaskedTextBox available, but I don't want to use it.
>
> > In summary, how do I tell the difference between a keystroke at the
> > end of the string versus a keystroke that replaces a character in the
> > string?
>
> > Thank you- Hide quoted text -
>
> - Show quoted text -

Reply via email to