Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Allow only alphanumeric in textbox.

Allow only alphanumeric in textbox.

Scheduled Pinned Locked Moved WPF
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prajeesh
    wrote on last edited by
    #1

    I have a textbox and it need the user not allow to enter any specialcharecters He can enter 1. A-Z                   2. a-z                   3. 0-9                   4. Space. How can I make the KeyDown event to do this?

    P K 2 Replies Last reply
    0
    • P Prajeesh

      I have a textbox and it need the user not allow to enter any specialcharecters He can enter 1. A-Z                   2. a-z                   3. 0-9                   4. Space. How can I make the KeyDown event to do this?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Rather than use KeyDown, try PreviewKeyDown on this instead. More importantly though, are you sure you've thought this through properly? What happens if the user presses the backspace or Delete key? What about the Tab key?

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        Rather than use KeyDown, try PreviewKeyDown on this instead. More importantly though, are you sure you've thought this through properly? What happens if the user presses the backspace or Delete key? What about the Tab key?

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        P Offline
        P Offline
        Prajeesh
        wrote on last edited by
        #3

        What I need is he can't enter any special characters in the textbox.

        P 1 Reply Last reply
        0
        • P Prajeesh

          What I need is he can't enter any special characters in the textbox.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          How do you define a special character? You need to think about your requirements a bit more. For instance, what about an apostrophe? If you're masking inputs like this, you wouldn't allow my surname through - and that would seriously annoy me (to the point where I probably wouldn't use your product).

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • P Prajeesh

            I have a textbox and it need the user not allow to enter any specialcharecters He can enter 1. A-Z                   2. a-z                   3. 0-9                   4. Space. How can I make the KeyDown event to do this?

            K Offline
            K Offline
            Kschuler
            wrote on last edited by
            #5

            I like to use the KeyPress event. Here is a sample that allows only alpha characters, numbers, and the control keys like enter, tab, backspace, delete, etc.

            Private Sub txtMyTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMyTextbox.KeyPress
            If Char.IsDigit(e.KeyChar) OrElse Char.IsControl(e.KeyChar) OrElse Char.IsLetter(e.KeyChar) Then
            e.Handled = False
            Else
            e.Handled = True
            End If
            End Sub

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups