Results 1 to 9 of 9

Thread: Textbox to accept letters and numbers only

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Textbox to accept letters and numbers only

    Hi guys help please! how will i make my textbox to accept letters and numbers only? thanks in advance!

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Textbox to accept letters and numbers only

    Try this:

    csharp Code:
    1. public class NumericTextBox : TextBox
    2. {
    3.     public NumericTextBox() { }
    4.  
    5.     protected virtual int[] AlwaysValid
    6.     {
    7.         get { return new int[] { 1, 3, 8, 13, 22, 24 }; } //Allow paste, copy, backspace etc...
    8.     }
    9.  
    10.     public override string Text
    11.     {
    12.         get
    13.         { return base.Text; }
    14.         set
    15.         {
    16.             if (value.ToCharArray().Where((char c) => !char.IsLetterOrDigit(c)).Count() == 0)
    17.                 base.Text = value;
    18.         }
    19.     }
    20.  
    21.     protected override void WndProc(ref Message m)
    22.     {
    23.         switch (m.Msg)
    24.         {
    25.             case 0x302:
    26.                 string pasteText = Clipboard.GetText();
    27.                 if (pasteText.ToCharArray().Where((char c) => !char.IsLetterOrDigit(c)).Count() > 0)
    28.                 {
    29.                     return; //Invalid paste string
    30.                 }
    31.                 break;
    32.             case 0x102:
    33.             case 0x106:
    34.             case 0x286:
    35.                 char keyChar = (char)m.WParam;
    36.                 if (!char.IsLetterOrDigit(keyChar) &&
    37.                     (Array.IndexOf(this.AlwaysValid, m.WParam.ToInt32()) == -1))
    38.                 {
    39.                     return; //Invalid key char press
    40.                 }
    41.                 break;
    42.         }
    43.         base.WndProc(ref m);
    44.     }
    45. }

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Textbox to accept letters and numbers only

    Hey,

    The above will give you a Numeric Textbox only, however, I think what you are looking for is an alpha numeric textbox, in which case you can simply add the following to the KeyPress Event Handler for the TextBox:

    Code:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    // Allows only Alpha-Numeric’s
    if (!(Char.IsLetter(e.KeyChar) || Char.IsDigit(e.KeyChar) || Char.IsControl (e.KeyChar)))
    e.Handled = true;
    }
    Hope that helps!!

    Gary

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Textbox to accept letters and numbers only

    No it's an alpha numeric text box. If you look at the code you will see 'char.IsLetterOrDigit'. The name is however misleading. Also your code does not account for paste attempts, or for the designer changing the text/lines property in design mode.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Textbox to accept letters and numbers only

    Hey,

    You are correct.

    What I have presented is simply a basic modification to the standard textbox, which will only validate key presses directly into the textbox while in run mode.

    The OP didn't specifically state which situations they wanted to account for though.

    Gary

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Textbox to accept letters and numbers only

    Quote Originally Posted by gep13
    The OP didn't specifically state which situations they wanted to account for though.
    True, but I think it's good practice to account for all situations.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Textbox to accept letters and numbers only

    Hey,

    You are correct, I think I was just being lazy

    One other thing that you might want to think about is the use of the MaskedTextBox:

    http://msdn.microsoft.com/en-us/libr...edtextbox.aspx

    Depending on what exactly you are trying to achieve, you may be able to do it with the Mask Property.

    Gary

  8. #8

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Textbox to accept letters and numbers only

    Thanks guys! may be i'll use what forumaccount suggested but thanks gep!

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Textbox to accept letters and numbers only

    Hey,

    Not a problem with that.

    If your question has been answered though, remember to mark the thread as resolved.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width