473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

character supression in a TextBox

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
Nov 21 '05 #1
7 3063
Have you tried handling the TextBox.TextChanged event?

Terp
"Itar" <It**@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
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

Nov 21 '05 #2
"Itar" <It**@discussions.microsoft.com> schrieb:
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/groups?selm=OFw5dO1kEHA.2504%40TK2MSFTNGP14.phx.gb l>
(= <news:OF**************@TK2MSFTNGP14.phx.gbl>)

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
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
Nov 21 '05 #4
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

"Terp" wrote:
Have you tried handling the TextBox.TextChanged event?

Terp
"Itar" <It**@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
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


Nov 21 '05 #5
Thanks,
I still need to handle the condition on the user pasting invalid characters.

Itar

"Nitin" wrote:
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

Nov 21 '05 #6
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

"Herfried K. Wagner [MVP]" wrote:

My suggestion is not to suppress certain characters:

<URL:http://groups.google.de/groups?selm=OFw5dO1kEHA.2504%40TK2MSFTNGP14.phx.gb l>
(= <news:OF**************@TK2MSFTNGP14.phx.gbl>)

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
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

"Itar" wrote:
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

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control,...
1
by: tshad | last post by:
I am trying to find the best way to delete the last character from a textbox. For example: status.text = "full/part/" I want to take out the trailing "/". Thanks,
0
by: Hal Gibson | last post by:
Because of a legacy (originally DOS) Sub Procedure "AlphaInput" that is called in thousands of places in our code, I need to be able to set a variable, "KeyedString",to the value of TextBox.Text...
1
by: Flack | last post by:
Hey guys, I have two questions regarding the vertical srollbar of a multiline textbox. 1. I have an "Ok" button that is initially disabled. I want it to be enabled when the user scrolls the...
2
by: Jassim Rahma | last post by:
how can point the cursor in the textbox after the last character?
1
by: vingomail | last post by:
Hi friends.. I need a javascript function to validate a number or character for textbox in asp.net Please help me
1
by: The Last Danish Pastry | last post by:
In a C# program have a multiline input text box (with AcceptsTab = true). Two questions: 1) The "tab positions" are every 8 characters, how could I change that to 2? 2) Could I make the...
9
by: =?Utf-8?B?dHBhcmtzNjk=?= | last post by:
OK I have some Chinese text in sql server column that looks like this: 12大专题调研破解广东科学发展难题 This is unicode? Anyway, I put this data into a text area like this:...
4
by: HenrikL | last post by:
Hi! I have textbox that have fixed size in GUI and I need to know how many character I can put into it. For example, the textbox can contain 5 character in GUI and I wan to check if this string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.