InputBox: A simple user input dialog





0/5 (0 vote)
Consider the scenario where the input value must be valid.public static double? GetDouble(string caption, string defaultValue, bool mustBeValid = false){ ClearLastError(); using (InputForm inForm = new InputForm(caption, defaultValue)) { while (true) { ...
Consider the scenario where the input value must be valid.
public static double? GetDouble(string caption, string defaultValue, bool mustBeValid = false)
{
ClearLastError();
using (InputForm inForm = new InputForm(caption, defaultValue))
{
while (true)
{
if (inForm.ShowDialog() != DialogResult.Cancel)
{
if (inForm.StringValue != string.Empty)
{
try
{
//If valid value, we will break the loop.
return double.Parse(inForm.StringValue);
}
catch (Exception ex)
{
LastError = ex;
}
}
}
if(mustBeValid)
{
MessageBox.Show("You must enter a valid value");
} else
{
return null;
}
}
}
}