-1

I have developed a C# solution which requires that the user can customize the format of a real number that is displayed, with a caption, a number and an unit.

I allowed the user to specify a format string that exactly matches the first parameter of a string.Format(), so he can tune the display the way he wants.

For example, {0}: {1:0.00} [{2}] will display Flow rate: 123.32 [Nm³/h].

The users knows how to use this formatting feature, that {0} is the caption, {1} is the number and {2} is the digit, and have the required minimal knowledge about .NET formatting.

However, at some point I have to validate the format string he entered, and I did not find other way than to use it against dummy values and to catch a FormatException this way:

try
{
    string.Format(userFormat, "", 0d, "");

    // entry acceptance...
}
catch(FormatException)
{
    messageBox.Show("The formatting string is wrong");

    // entry rejection...
}

When an error happens, this is not the most user-friendly...

Could NET format strings be validated a more elegant way? Is there a way to provide some hints to the user in case of failure?

3
  • You can use ErrorProvider Class. Commented Apr 10, 2014 at 19:36
  • Try looking at this SO question Commented Apr 10, 2014 at 22:49
  • @icemanind oh, I missed that one. It is funny that the accepted answer is giving exactly the problem I am wondering about. I am probably wondering too much, it is probably why this question has been downvoted. Commented Apr 11, 2014 at 8:59

2 Answers 2

1

There is most likely a much better and more efficient way to do this out there. This is just one possible way.

But Here is what I came Up with.

public bool IsInputStringValid(string input)
    {
        //If there are no Braces then The String is vaild just useless
        if(!input.Any(x => x == '{' || x == '}')){return true;}

        //Check If There are the Same Number of Open Braces as Close Braces
        if(!(input.Count(x => x == '{') == input.Count(x => x == '}'))){return false;}

        //Check If Value Between Braces is Numeric

        var tempString = input; 

        while (tempString.Any(x => x == '{'))
        {
            tempString = tempString.Substring(tempString.IndexOf('{') + 1);
            string strnum = tempString.Substring(0, tempString.IndexOf('}'));

            int num = -1;
            if(!Int32.TryParse(strnum, out num))
            {
                    return false;
            }       
        }

        //Passes Validation
        return true;
    }

Here Is The Fiddle: http://dotnetfiddle.net/3wxU7R

Sign up to request clarification or add additional context in comments.

1 Comment

So, as I understood, there is no magic way to do this, without to implement his own validation logic and to parse the entry. Thanks for having took the time to suggest an example. You deserve your answer to be accepted.
0

Have you tried:

catch(FormatException fe)
{
     messageBox.Show("The formatting string is wrong: " + fe.Message);

// entry rejection...
}

1 Comment

Yes, unfortunately, it is "Input string was not in a correct format" 90% of the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.