character supression in a TextBox

G

Guest

I'm having a problem supressing characters in a text box. I only want alpha
numeric characters (no special chars). I can handle the TextBox_KeyPress
event to supress the invalid characters when the user types them in, however
that doesn't handle the user pasting invalid characters. I thought to use
the MouseDown event but I couldn't figure out how to tell if the user is
trying to paste or is copying the text. To paste the text the user would
have to right-click to pull down the context menu and select paste.

My question: How can I tell if the user is trying to paste text to the
textbox?
Is there an event raised on paste?
or do I have to tell the user selected paste from the context menu?
(ok, that's 3 questions) :)

Thanks in advance
 
H

Herfried K. Wagner [MVP]

Itar said:
I'm having a problem supressing characters in a text box. I only want
alpha
numeric characters (no special chars). I can handle the TextBox_KeyPress
event to supress the invalid characters when the user types them in,
however
that doesn't handle the user pasting invalid characters. I thought to use
the MouseDown event but I couldn't figure out how to tell if the user is
trying to paste or is copying the text. To paste the text the user would
have to right-click to pull down the context menu and select paste.

My suggestion is not to suppress certain characters:

<URL:http://groups.google.de/[email protected]>
(= <
 
N

Nitin

Here is a chunk of sample code you could add underneath a TextBox KeyPress
event to do this:

If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
' Cancel non-numeric characters
e.Handled = True
End If Another way to do this would be to create a list of allowable
characters. Here, for example, we're allowing numbers, spaces, colons, and
dashes:

Dim strAllowableChars As String
strAllowableChars = "0123456789-: "
' If the character they have entered isn't in our list...
If InStr(strAllowableChars, e.KeyChar.ToString) = 0 Then
' Cancel the character
e.Handled = True
End If Nitin
 
G

Guest

I have considered that, but to prevent the invalid characters from being
pasted into the textbox I have to search the entire text for invalid
characters every time the TextChanged event fires. If the user puts any
descent sized string in the textbox the TextChanged event will be terribly
inefficient.

Itar
 
G

Guest

Thanks,
I still need to handle the condition on the user pasting invalid characters.

Itar
 
G

Guest

That would be my preference as well. However, my requirements say I have to
supress them and I've had no luck getting them changed.

Itar
 
G

Guest

Here's what I had to do to supress invalid characters in a paste event. It's
probably not the best way but it works. There could be some efficiency
issues if the value of the textbox gets large but it shouldn't be a problem
very often for what I'm doing.

dim mblnIgnoreChanges as Boolean = False
dim mblnKeyStroke as Boolean = False

TextBox_KeyPress(...)
if char.isletterordigit(e.KeyChar) = false andalso
convert.toint32(e.KeyChar) <> 8 Then
e.Handled = True
else
mblnKeyStroke = True
end If
end sub

TextBox_TextChanged(...)
if not mblnKeyStroke andalso not mblnIgnoreChanges Then
' text changed by some event other than a keystroke.
dim strNewText As String = String.Empty
for each ch as Char in TextBox.Text
if char.IsLetterOrDigit(ch) = False Then
' do nothing
else
strNewText &= ch
end if
next
mblnIgnoreChanges = True
TextBox.Text = strNewText
mblnIgnoreChanges = False
End If
mblnKeyStroke = False
end Sub

Thanks everyone for your responses.

Itar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top