alfah
alfah

Reputation: 2085

How to validate( format 000.0) the textbox in WP7

I want to check the number format as the user types in each number in the following format Three digits before decimal and one digit after point(if any)

The moment the user enters the 3 digits, Im trying to add decimal point. Is there any event which is fired when user enters a number?

The TextInput and TextInputStart events do not work as expected. When i try to enter 332 it shows as 233. The following function is called on the TextInputStart event.

private void TestFunction(object sender, TextCompositionEventArgs e)
    {
        TextBox txtbox = e.OriginalSource as TextBox;
        string r = txtbox.Text;

        if(r.Contains('.'))
        {
            for (int i = 0; i < r.Length; i++)
            {
                if (r.Substring(i, 1) == ".")
                {
                    txtbox.Text = r.Substring(0, i + 2);

                }
            }
        }

        if (r.Length == 2 && r[2] != '.')
        {
            r += ".";
            txtbox.Text = r;
        }

    }

While debuggin i noticed that, TextInputStart is fired and the text box has the previous string and not the last entered string.

Any way out? :(

Upvotes: 1

Views: 629

Answers (4)

alfah
alfah

Reputation: 2085

I used regular expressions to check 3 digits before decimal and one digit after decimal. The following function is called on the key up event of the textbox. Saves a lot of time and can avoid the unnecessary use of messagebox. If anybody is looking out for an answer, here is mine.

 private void ValidateValue(object sender, KeyEventArgs e)
    {
        string sInput, sPattern;

        TextBox txtbox = e.OriginalSource as TextBox;
        sInput = txtbox.Text;

        sPattern = @"^([0-9]{0,3})?(\.([0-9]{1})?)?$";
        Regex reg = new Regex(sPattern);


        if (reg.IsMatch(sInput))
        {
            if (sInput != "" && (sInput[0] == '.' || sInput[0] == '0'))
            {
                txtbox.Text = "";
                return;
            }

            txtbox.Text = sInput;
        }
        else
        {
            txtbox.Text = sInput.Substring(0, sInput.Length - 1);
            txtbox.Select(txtbox.Text.Length, 0);
        }
    }

Upvotes: 0

Cyber1000
Cyber1000

Reputation: 21

I'm not quite sure if this applies also to WP7, but as it looks like C# so I'll give it a try. In .NET you also have a MaskedTextBox, where you could apply a Mask with 0.00 . The benefit: Your user may only input numbers of the given format and you don't have to code anything.

Upvotes: 0

Kevin Gosse
Kevin Gosse

Reputation: 39007

The TextInputStart event is triggered before the TextBox is updated, that's why it still contains the old text. Instead of reading txtbox.Text, use e.Text.

Upvotes: 1

Eugene
Eugene

Reputation: 2985

You could use OnKeyDown event, measure length of text, and if it is 3, add the decimal. You can also check the actual entered value using Regular Expressions or just set InputScope property on the TextBox to only provide the numeric keyboard for that particular TextBox.

If you do decide to add a decimal point yourself, then you may want to look at this page here, that describes how you move the cursor to the end of the text:

tbPositionCursor.Select(tbPositionCursor.Text.Length, 0);

Here is how you might want to handle the OnKeyDown event:

private void TextBox1_OnKeyDown(object sender, KeyEventArgs e)
    {
        TextBox t = sender as TextBox;;
        if (e.Key == ...

So you are not checking the Text property of a TextBox, but rather the Key property of the KeyEventArgs.

Upvotes: 1

Related Questions