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 use the KeyPress event

How to use the KeyPress event

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
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.
  • K Offline
    K Offline
    kharr1027
    wrote on last edited by
    #1

    Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.

    N B 2 Replies Last reply
    0
    • K kharr1027

      Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Something like this should get you started:

      private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
      // Allow only alpha characters
      base.OnKeyPress(e);
      if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
      || Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
      e.Handled = false;
      else
      e.Handled = true;
      }

      -Nick Parker

      K 1 Reply Last reply
      0
      • N Nick Parker

        Something like this should get you started:

        private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
        // Allow only alpha characters
        base.OnKeyPress(e);
        if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
        || Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
        e.Handled = false;
        else
        e.Handled = true;
        }

        -Nick Parker

        K Offline
        K Offline
        kharr1027
        wrote on last edited by
        #3

        well thanks for the help anyways but it doesn't work. i can still type numbers and wierd characters into the text box. Does anyone else have any ideals?here is what my teacher showed us in class but i can't get anything out of it? thanks private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; }

        N 1 Reply Last reply
        0
        • K kharr1027

          Hello, I am a newbie at C#. I am trying to make a text box only allow alpha characters, such as a - z upper and lower case, the backspace, and space keys. How would i write that in the keypress event and how do i get it to activate on that text box? thanks any help will be appreciated.

          B Offline
          B Offline
          Bo Hunter
          wrote on last edited by
          #4

          Character codes. Look them up and compare them. Filter out what you dont want. You set e.Handled to true when you dont want the character entered. Other wise if it is valid e.Handled = false;

          1 Reply Last reply
          0
          • K kharr1027

            well thanks for the help anyways but it doesn't work. i can still type numbers and wierd characters into the text box. Does anyone else have any ideals?here is what my teacher showed us in class but i can't get anything out of it? thanks private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; }

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            kharr1027 wrote: private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar < 'a'||e.KeyChar > 'z')&& e.KeyChar != '\b') e.Handled = true; } A few things, first of all I just re-tried the code example I gave you and it does work, however I noticed in the code example you listed you have Form1_KeyPress which appears that you have an event handler registered for the KeyPress event of a Form, not a TextBox. Make sure whatever the name of your textbox that you add to your form includes the following event/delegate assignment (for this example the name of my textbox is simply the default of textBox1):

            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);

            then the following will work to only allow alphabetic characters, space and backspace entries:

            private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
            // Allow only alpha characters
            base.OnKeyPress(e);
            if (Char.IsLetter(e.KeyChar) || Char.IsPunctuation(e.KeyChar)
            || Char.IsSeparator(e.KeyChar) || Char.GetNumericValue(e.KeyChar) == -1)
            e.Handled = false;
            else
            e.Handled = true;
            }

            -Nick Parker

            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