Field level validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yuelin

    #1

    Field level validation

    Hi all

    I want to validate the textbox while user entering each character, I wrote:

    private void txtAge_KeyPress (object sender,
    System.Windows. Forms.KeyPressE ventArgs e)
    {
    if (Char.IsLetter( e.KeyChar) == true)
    {
    MessageBox.Show ("Age should not contain a letter" );
    }
    }


    So whenever the user enters a character rather than a digit, the system will
    pop up a message box.
    But when clicking the OK button on the message box, the character is still
    entered.

    How can I prevent the character being entered when clciking the OK button?

    Thanks

    Yuelin


  • Jon Skeet

    #2
    Re: Field level validation

    Yuelin <liang@lineone. net> wrote:[color=blue]
    > I want to validate the textbox while user entering each character, I wrote:
    >
    > private void txtAge_KeyPress (object sender,
    > System.Windows. Forms.KeyPressE ventArgs e)
    > {
    > if (Char.IsLetter( e.KeyChar) == true)
    > {
    > MessageBox.Show ("Age should not contain a letter" );
    > }
    > }
    >
    >
    > So whenever the user enters a character rather than a digit, the system will
    > pop up a message box.
    > But when clicking the OK button on the message box, the character is still
    > entered.
    >
    > How can I prevent the character being entered when clciking the OK button?[/color]

    Setting e.Handled=true should do it, I believe.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...