Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Filtering Textbox input

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny Jörgensen - 23 Apr 2008 12:03 GMT
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.
Kerry Moorman - 23 Apr 2008 13:00 GMT
Johnny,

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

Kerry Moorman

> I've got a textbox where I filter the input in the KeyPress event to allow
> only certain characters.
[quoted text clipped - 6 lines]
> TIA,
> Johnny J.
kimiraikkonen - 23 Apr 2008 13:24 GMT
On Apr 23, 3:00 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.com> wrote:
> Johnny,
>
[quoted text clipped - 13 lines]
> > 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
Claes Bergefall - 24 Apr 2008 11:43 GMT
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

> I've got a textbox where I filter the input in the KeyPress event to allow
> only certain characters.
[quoted text clipped - 6 lines]
> TIA,
> Johnny J.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.