Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

textfield validation in C#

Options
  • 16-02-2004 6:54pm
    #1
    Closed Accounts Posts: 88 ✭✭


    Does anyone know how to stop a user from entering letters into a textfield that only requires numeric values e.g when a persons age is required?

    I could validate the textfield afterwards but I want to know if it is possible to disable the letter keys, thus preventing a user from entering invalid values in the first place.


Comments

  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    You have to write the code yourself. The best way would probably to subclass a textbox to a new control which only allows numerics, so then you could use the control at will.

    a google on 'c# numberic textbox' should get you started, but you'll also find its not as simple as you might think.

    Preventing the user from typing it in is easy. Preventing Ctrl-V or Shift-Insert (both paste) also has to be considered, as does Right-Click/Paste with the context menu.

    jc


  • Registered Users Posts: 79 ✭✭tendofan


    I've found this to work:

    In the Keypress event handler for the text box, put the following:

    if (32 <= (int)e.KeyChar &&
    -1 == "0123456789".IndexOf(e.KeyChar))
    {
    e.Handled = true;
    }

    Basically, if the character has a value greater than 32, it's printable character, and if it isn't one of the numbers, then you set the handled property to true, which stops the character being added to the textbox.

    Best thing to do is to inherit from the textbox control, implementing that keypress event.

    Hope this helps

    Tendofan.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Yup.

    YOu can easily extend this to handle the decimal point (although it takes a teeny bit more smarts to ensure that only one decimal point is entered), the + and - signs, and so on.

    But like I said....that still won't prevent someone from usiing the mouse to cut n paste into field.

    jc


  • Registered Users Posts: 79 ✭✭tendofan


    Yeah, I know, I just thought that would get the ball rolling. :-)

    Ideally you'd intercept WM_PASTE messages to the text-box, but if the amount of data in the text box is going to be small, most-likely with numeric data, then you can do this:

    private m_sAllowed = "0123456789"; //& whatever you're having yourself
    private m_sPrevious = "";

    and in the textChanged event:

    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    bool bOK = true;

    foreach (char ch in textBox1.Text)
    {
    if (-1 == m_sAllowed.IndexOf(ch))
    {
    bOK = false;
    break;
    }
    }

    if (bOK)
    {
    m_sPrevious = textBox1.Text;
    }
    else
    {
    textBox1.Text = m_sPrevious;
    }

    return;
    }

    It's not the most elegant by any means, but it gets you out of a hole. If you combine it with the keypress stuff from before it's not all _that_ bad, though it is better to override WndProc, check for WM_PASTE (message 0x302) and go test only the data on the clipboard.

    Tendofan


Advertisement