Click here to Skip to main content
12,300,878 members (66,292 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.8K views
1 bookmarked
Posted

InputBox: A simple user input dialog

, 1 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
So here is a tip on how to improve upon this code. Do not silently catch exceptions! Instead, create a very simple logging mechanism that allows you to inspect the exception, either from code or during debugging.public static class InputBox{ public static Exception LastError {get; private...

So here is a tip on how to improve upon this code. Do not silently catch exceptions! Instead, create a very simple logging mechanism that allows you to inspect the exception, either from code or during debugging.

public static class InputBox
{
  public static Exception LastError {get; private set;}
  public static void ClearLastError(){ LastError = null;}
 
  public static double? GetDouble(string caption, string defaultValue)
  {
     ClearLastError();
     using (InputForm inForm = new InputForm(caption, defaultValue))
     {
            if (inForm.ShowDialog() == DialogResult.Cancel) return null;
            if (inForm.StringValue == string.Empty) return null;
            try { return double.Parse(inForm.StringValue); }
            catch (Exception ex)
            { 
               LastError = ex;
               return null; 
            }
     }
  }  
}

Contrived usage:

double? value = InputBox.GetDouble("xyz", "1.0");
if(!value.HasValue && InputBox.LastError is OutOfMemoryException)
{
    // holy ship, batman!
}

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

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 1 Dec 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid