tags:

views:

399

answers:

6

I need a textbox which only the user can permit to enter integers. But the user can't enter zero. i.e, he can enter 10,100 etc. Not 0 alone. How can I make event in KeyDown?

+3  A: 

Use int.TryParse to convert the text into a number and check if that number is not 0. Use the Validating event for the check.

// this goes to you init routine
textBox1.Validating += textBox1_Validating;

// the validation method
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if (textBox1.Text.Length > 0)
    {
        int result;
        if (int.TryParse(textBox1.Text, out result))
        {
            // number is 0?
            e.Cancel = result == 0;
        }
        else
        {
            // not a number at all
            e.Cancel = true;
        }
    }
}

EDIT:

Okay, since you use WPF you should take a look at how to implement validation the WPF way. Here is a validation class that implements the above logic:

public class StringNotZeroRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (textBox1.Text.Length == 0)
            return new ValidationResult(true, null);

        int result;
        if (int.TryParse(textBox1.Text, out result))
        {
            // number is 0?
            if (result == 0)
            {
                return new ValidationResult(false, "0 is not allowed");
            }
        }
        else
        {
            // not a number at all
            return new ValidationResult(false, "not a number");
        }

        return new ValidationResult(true, null);
    }
}
VVS
My case is in WPF.
Sauron
+6  A: 

Why not using a NumericUpDown and make the following settings:

upDown.Minimum = 1;
upDown.Maximum = Decimal.MaxValue;
Oliver
I don't believe WPF has a NumericUpDown control.
Joel Cochran
I didn't work with WPF till today and i just didn't see this tag in his question.In this case i would sort out all non numerical inputs in the key down event and check for zero in OnValidating (as already mentioned by bart)
Oliver
A: 
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (textBox1.Text == "" && e.KeyChar == '0')
        {
            e.Handled = true;
            return;
        }
        if (e.KeyChar < '0' || e.KeyChar > '9')
        {
            e.Handled = true;
            return;
        }

    }

not nice but it works

millapit
This would allow entering "10" and then deleting the "1".
VVS
doesn't let you back space
vaquito
+1  A: 

This is another variation on the theme:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    char newChar = Convert.ToChar(e.KeyValue);
    if (char.IsControl(newChar))
    {
        return;
    }
    int value;
    e.SuppressKeyPress = int.TryParse((sender as TextBox).Text + newChar.ToString(), out value) ? value == 0 : true;
}
Fredrik Mörk
Sauron
Ah, missed the wpf tag, my bad.
Fredrik Mörk
+8  A: 

The way you plan to do this, is very annoying for a user. You're guessing what a user wants to enter, and act upon your guess, but you can be so wrong.

It also has holes, for example, a user can enter "10" and then delete the "1". Or he could paste in a "0" -- you do allow paste, don't you?

So my solution would be: let him enter any digit he likes, any way he likes, and validate the input only after he finished, for example, when the input loses focus.

bart
Now I am doing that. But I will find a solution for this. Lets try.
Sauron
That's exactly why my solution uses the validation event and ignores the key* event as suggested. Validate only when the user has finished the input.
VVS
A: 

Blockquote![

  1. alt text

][1]

I can't understand your answer.
Sauron