473,385 Members | 1,753 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,385 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 5687
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.