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
  1. Home
  2. General Programming
  3. C#
  4. How to restrict the user entering some invalide keys in TestBox

How to restrict the user entering some invalide keys in TestBox

Scheduled Pinned Locked Moved C#
tutorial
3 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.
  • E Offline
    E Offline
    engsrini
    wrote on last edited by
    #1

    I am developing Login Control where the user can enter username and password. in the Username and password box i should restrict the user from entering Special Chars, and other invalid keys. i know i can compare the the value in Keydown event using Keys.Keycode, but it is very ugly to put all the Spl char comparision like if(e.Keycode==Keys.Enter || etc..). Is there any function or way to Compare the key pressed is a spl. char or not. Thanks and Regards Srini

    A L 2 Replies Last reply
    0
    • E engsrini

      I am developing Login Control where the user can enter username and password. in the Username and password box i should restrict the user from entering Special Chars, and other invalid keys. i know i can compare the the value in Keydown event using Keys.Keycode, but it is very ugly to put all the Spl char comparision like if(e.Keycode==Keys.Enter || etc..). Is there any function or way to Compare the key pressed is a spl. char or not. Thanks and Regards Srini

      A Offline
      A Offline
      Anh_Tuan
      wrote on last edited by
      #2

      Try MaskedTextBox! But filtering using KeyDown is still my preference. -K Jup

      1 Reply Last reply
      0
      • E engsrini

        I am developing Login Control where the user can enter username and password. in the Username and password box i should restrict the user from entering Special Chars, and other invalid keys. i know i can compare the the value in Keydown event using Keys.Keycode, but it is very ugly to put all the Spl char comparision like if(e.Keycode==Keys.Enter || etc..). Is there any function or way to Compare the key pressed is a spl. char or not. Thanks and Regards Srini

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        Think outside the box!!!!! You have a group of keys that you want to have disallowed from entry in a textbox. So a simple solution to this would be something like this:

         List invalidKeys = new List();
                 ...
         private void ThisForm\_Load()
         {
              invalidKeys.Add(Keys.Enter);
              invalidKeys.Add(Keys.Ctl);
               ...
         }
        
                ...
         // keypress event handler
              if ( invalidKeys.Contains(e.Keycode) )
                  e.Handled = true;
        

        -- modified at 9:49 Thursday 13th July, 2006

        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