By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,977 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,977 IT Pros & Developers. It's quick & easy.

Filtering Textbox input

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.