How To Cancell Character Entry in a text box?

  • Thread starter Thread starter Dino Buljubasic
  • Start date Start date
D

Dino Buljubasic

Hi,
I'd like to be able to enter only letters in my text box. If a user
enters anything except a letter, the key entered would simply not be
displayed in the text box.j
How can I do ;that?

Thank you
 
I got this one by myself :

Inside text box KeyPress events do :

if (char.IsLetter(e.KeyChar) = false) then
e.Handled = true
end if

Thanks anyways
 
* Dino Buljubasic said:
I'd like to be able to enter only letters in my text box. If a user
enters anything except a letter, the key entered would simply not be
displayed in the text box.j

I would not restrict the user to enter certain characters. Instead, I
would add a handler to its 'Validating' event and check the content of
the textbox there. If it's not "valid", you can set an errorprovider on
that control.
 
Herfried:

I'm interested. Why are you advocating asking the user to make an entry,
letting them type whatever they like, and then--only after they've
finished--telling them that they can't use what they've typed?

Thanks,
Russell Jones,
Executive Editor, DevX.com
 
* "Russell Jones said:
I'm interested. Why are you advocating asking the user to make an entry,
letting them type whatever they like, and then--only after they've
finished--telling them that they can't use what they've typed?

For example, if I copy something to the clipboard and paste it into the
textbox, then edit it to make it "valid". Preventing the user from doing
that (pasting/editing) whatever he/she wants, is IMO annoying. That's
why the ErrorProvider component was made available.

Just my 2 Euro cents.
 
For example, if I copy something to the clipboard and paste it into the
textbox, then edit it to make it "valid". Preventing the user from doing
that (pasting/editing) whatever he/she wants, is IMO annoying. That's
why the ErrorProvider component was made available.

And therefore I say always that doing both is the best.

Retyping a full textbox after 10000 characthers is anoying.

Cor
 
* "Cor Ligthert said:
And therefore I say always that doing both is the best.

Mhm... I want a great user experience without restrictions.
Retyping a full textbox after 10000 characthers is anoying.

Who said that you must do that?
 
Herfried:

I agree with you that pasting and editing out invalid characters is a
convenient feature. But trapping the Keypress event (as the original
poster's code does) to prevent users from *typing* invalid characters
doesn't prevent users from pasting text in--nor from editing it. Of course
you should validate the completed entry as well.... So my original question
still stands: Do you still feel that preventing users from entering invalid
characters should only be done at the end of the edit?

Thanks,
Russell
 
I guess, the right way would be to do both but in my case, the text
box is actually a search entry, each time a user enters a letter, on
keyUp a search will be performed to find names starting with the
letter or letters entered.

Therefore, probably the best would be to do it as I did, to simply
ignore all characters that are not letters (except the Backspace)

Dino
 
* "Russell Jones said:
I agree with you that pasting and editing out invalid characters is a
convenient feature. But trapping the Keypress event (as the original
poster's code does) to prevent users from *typing* invalid characters

I agree, but I am afraid that this may confuse users.
doesn't prevent users from pasting text in--nor from editing it. Of course
you should validate the completed entry as well.... So my original question
still stands: Do you still feel that preventing users from entering invalid
characters should only be done at the end of the edit?

That's my personal opinion, but I know that it has its disadvantages.
When editing very complex strings, using a masked edit control or a
datetimepicker will be much easier, but it will prevent the user from
in-place editing.
 
Inside text box KeyPress events do :

if (char.IsLetter(e.KeyChar) = false) then
e.Handled = true
end if

A little simpler would be:

e.Handled = Not Char.IsLetter(e.KeyChar)
 
* Chris Dunaway said:
A little simpler would be:

e.Handled = Not Char.IsLetter(e.KeyChar)

.... but this would assign a value to 'e.Handled' even if it's not
necessary...

SCNR ;-)
 
Back
Top