Make the user enter a float in C#


The posts Preview TextBox changes in C# and Preview TextBox changes in C#, Part 2 show how you can make a TextBox accept only integers. This example is similar except it makes a TextBox only accept floats.

This change is remarkably simple. (Almost like I planned it that way, huh?)

The key method that determines whether a new TextBox value is valid is the ValueIsValid method. In the previous examples, that method returns true if the new value is a valid integer or the beginning of one as in – or +. The new version of this method returns true if the value looks like a float or the beginning of one.

The code can use float.TryParse to determine if the test value is actually a float but that won’t always work if the user is in the process of typing the new value. For example, suppose the user wants to type -9e-8. This is a valid float but at some point the user will have typed -9e-, which is not a valid value.

You could try to build a state machine that determines whether the test string could be the start of a valid float value, but you would need to handle a bunch of cases including exponential notation in various partially complete stages in addition to fixed-point values values such as -3.14.

Fortunately there is an easier method. If the test string ends in a non-digit, then just add a digit to the end and see if the result is a valid float. For example, -9e- becomes -9e-0, which is weird but valid. Similarly – and + become -0 and +0, which are also weird but valid.

The following version of ValueIsValid uses this approach.

// Return true if the value is a valid integer.
private bool ValueIsValid(string text_value)
    // Do not allow spaces.
    if (text_value.Contains(' ')) return false;

    // Allow a blank value.
    if (text_value.Length == 0) return true;

    // If the text doesn't end in a digit, add a digit
    // to see if it is a valid prefix of a float.
    if (!char.IsDigit(text_value, text_value.Length - 1))
        text_value += "0";

    // See if the text parses.
    float test_value;
    return float.TryParse(text_value, out test_value);

This method rejects the value if it contains space characters and it accepts a blank value.

Next if the value doesn’t end in a digit, the code appends a 0. The method then uses float.TryParse to see if the result is a valid float.

Note that the code still allows some slightly confusing values. For example, 1e100 is not a valid float because it is too large (it is a valid double) but 1e-100 is valid because it is essentially 0.0.

The previous examples let the user enter only integers and this example lets the user enter only floats. You can easily modify the ValueIsValid method to make the user enter other data types such as byte, long, ulong, double, decimal, and others. With a little work, you could even restrict the user to Boolean values such as True, False, Yes, and No.

This example and the earlier ones use code that is hard-wired to a particular TextBox. My next post explains how to make the code easier to use more easily with multiple TextBox controls.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in controls, user interface and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *