Results 1 to 4 of 4

Thread: Textbox AlphaNumeric with windows special keys?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Textbox AlphaNumeric with windows special keys?

    Hi all,

    I was looking for a textbox that can hold alphanumerics only and one can also use windows shortcuts such as control+v Control+c Control+a within that textbox.
    I tried this in my keypress event for the text box and it works, but i would like for it to also allow the other windows shortcut functions

    6 Code:
    1. if (!(Char.IsLetterOrDigit(e.KeyChar) == true || (e.KeyChar) == 8))
    2.             {
    3.                 e.Handled = true;
    4.             }


    Take care

    P.s. I would like to not use the masked text box.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Textbox AlphaNumeric with windows special keys?

    Use the Char.IsControl method to test for control characters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Re: Textbox AlphaNumeric with windows special keys?

    Thanks jmc

    This is what i have so far and it seems to work.

    Code:
    protected override void OnKeyPress(KeyPressEventArgs e)
            {
                if ((Char.IsControl(e.KeyChar) == true))
                {
                    e.Handled = false;
                }
                else if (!(Char.IsLetterOrDigit(e.KeyChar) == true || (e.KeyChar) == 8))
                {
                    e.Handled = true;
                }        
    }

    Is there anyway i can check to validate something that is pasted into a textbox as well? i.e. if someone pastes axx@!!#333321 it shouldnt let them paste it into the textbox.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Textbox AlphaNumeric with windows special keys?

    There's not really a simple way to do that. You could test for the Ctrl+V character or key combination and then validate the contents of the clipboard. That doesn't stop the user right-clicking the control and selecting Paste from the standard menu though. If you've overridden the standard menu then that's OK, but otherwise you would have to override the WndProc method and get your hands a bit dirtier.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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