Click here to Skip to main content
11,577,258 members (58,894 online)
Click here to Skip to main content

Tagged as

InputBox: A simple user input dialog

, 1 Dec 2011 CPOL 3.7K
Rate this:
Please Sign up or sign in to 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;
          }
      }
   }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kabwla.Phone
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.

Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.

Either being an appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

You may also be interested in...

Comments and Discussions

 
Generaltesing. Pin
VICKY44341726-Dec-11 23:08
memberVICKY44341726-Dec-11 23:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid