Filtering Textbox input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?Q?Johnny_J=F6rgensen?=

    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.

  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Filtering Textbox input

    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.
    >

    Comment

    • kimiraikkonen

      #3
      Re: Filtering Textbox input

      On Apr 23, 3:00 pm, Kerry Moorman
      <KerryMoor...@d iscussions.micr osoft.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_TextCh anged(ByVal sender As System.Object, ByVal
      e As System.EventArg s) Handles TextBox1.TextCh anged
      If TextBox1.Text.C ontains("foo") = True Then
      MsgBox("you cannot paste that")
      ' Clear all unwanted entry
      TextBox1.Clear( )
      End If
      End Sub

      Hope this helps,

      Onur

      Comment

      • Claes Bergefall

        #4
        Re: Filtering Textbox input

        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(B yRef 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.GetDa taObject
        If data Is Nothing Then
        Return MyBase.ProcessC mdKey(msg, keyData)
        Else
        Dim text As String = CStr(data.GetDa ta(DataFormats. StringFormat,
        True))
        If String.IsNullOr Empty(text) Then
        Return MyBase.ProcessC mdKey(msg, keyData)
        Else
        For Each ch As Char In text.ToCharArra y
        If Not IsValidChar(ch) Then
        Return True
        End If
        Next
        Return MyBase.ProcessC mdKey(msg, keyData)
        End If
        End If
        Else
        Return MyBase.ProcessC mdKey(msg, keyData)
        End If
        End Function

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


        /claes

        "Johnny Jörgensen" <jojo@altcom.se wrote in message
        news:03E6B5A7-C850-496B-A9DD-34A9B50FFB0D@mi crosoft.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.

        Comment

        Working...