How to validate float variable in wpf textbox in both positive and negative

Dinesh Kumar R 1 Reputation point
2022-04-21T16:50:13.017+00:00

I tried with regular expression, it is not working, is there any pre-defined api, or binding with textbox or textbox property

Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-04-21T18:05:24.617+00:00

    The validation can be done in this manner:

    float f;
    if( ! float.TryParse( textBox1.Text, out f ) )
    {
        // Error. Show an error message or indicator
        // . . .
    }
    else
    {
        // Success
        // . . .
    }
    

    It can be done after pressing the action button like "OK", "Save", etc.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.