473,396 Members | 1,799 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,396 software developers and data experts.

Filtering Textbox input

I've got a textbox where I filter the input in the KeyPress event to allow
only certain characters.

However, If the user cuts and pastes a text into the textbox, all text is
entered - not only the characters allowed. That doesn't surprise me, but
what is the best way of avoiding that and still only allowing certain
characters even when pasting???

TIA,
Johnny J.

Jun 27 '08 #1
3 5384
Johnny,

One option would be to use a MaskedTextBox control instead of a TextBox
control.

Kerry Moorman
"Johnny Jörgensen" wrote:
I've got a textbox where I filter the input in the KeyPress event to allow
only certain characters.

However, If the user cuts and pastes a text into the textbox, all text is
entered - not only the characters allowed. That doesn't surprise me, but
what is the best way of avoiding that and still only allowing certain
characters even when pasting???

TIA,
Johnny J.
Jun 27 '08 #2
On Apr 23, 3:00 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
Johnny,

One option would be to use a MaskedTextBox control instead of a TextBox
control.

Kerry Moorman

"Johnny Jörgensen" wrote:
I've got a textbox where I filter the input in the KeyPress event to allow
only certain characters.
However, If the user cuts and pastes a text into the textbox, all text is
entered - not only the characters allowed. That doesn't surprise me, but
what is the best way of avoiding that and still only allowing certain
characters even when pasting???
TIA,
Johnny J.
Johnny,
You can handle textbox's textchanged event to restrict unwanted word/
letter entry.

For example; Assuming you don't want "foo" word to be pasted into your
textbox then clear textbox after unwanted word's entry, you can do:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Contains("foo") = True Then
MsgBox("you cannot paste that")
' Clear all unwanted entry
TextBox1.Clear()
End If
End Sub

Hope this helps,

Onur

Jun 27 '08 #3
Inherit it and override ProcessCmdKey. The exact code depends on if you want
to abort the pasting if it contains invalid characters or if you want to
strip them out and paste the rest. The following does the former (i.e.
aborts the paste if it contains any invalid character):

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal
keyData As Keys) As Boolean
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control
Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String = CStr(data.GetData(DataFormats.StringFormat,
True))
If String.IsNullOrEmpty(text) Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not IsValidChar(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function

Private Function IsValidChar(ByVal ch As Char) As Boolean
'TODO: Return True or False depending on the validity of the character
End Function
/claes

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:03**********************************@microsof t.com...
I've got a textbox where I filter the input in the KeyPress event to allow
only certain characters.

However, If the user cuts and pastes a text into the textbox, all text is
entered - not only the characters allowed. That doesn't surprise me, but
what is the best way of avoiding that and still only allowing certain
characters even when pasting???

TIA,
Johnny J.

Jun 27 '08 #4

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

Similar topics

3
by: Aionius | last post by:
Good day to all. I have this problem in JavaScript. I have a form (textbox) which accepts a series of numbers in format 9999-9999-9. Now i want to filter all inputs to the textbox. let's say...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
4
by: Dave | last post by:
I am having difficulty filtering a form as the user types in a onchange event here is my code strFilter = cboCriteria.Value & " LIKE '" & Me!txtCriteria.text & "*" & "'" If Len(strFilter ) 0...
2
by: Jimmy | last post by:
Hi! I'm looking for a library with methods for filtering user input in a web application. I mean, some API which gives more functionality than the traditional .NET validation controls. Any...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
0
by: Village Idiot | last post by:
I have: A textbox (txtLetter) A button (btnDisplay) A listbox (lstColors) On run time, the form automatically creates an Array and gets information from a .txt file. What I'm trying to do...
5
by: RHooper | last post by:
Hi, I'm new to Access, so I apologize if this question is trivial. I am trying to set-up a quick filter for users to define on a form bound to a table. I have a combo box called...
3
by: =?iso-8859-1?Q?Johnny_J=F6rgensen?= | last post by:
I've got a textbox where I filter the input in the KeyPress event to allow only certain characters. However, If the user cuts and pastes a text into the textbox, all text is entered - not only...
1
by: rakeshnair | last post by:
i wrote a code in jsp to create dynamic table..the problem is i need data base connection when cursor moves from one cell to other... eg...when i enter product id in the first cell, the product name...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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 project—planning, coding, testing,...

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.