title: Windows Forms - customizing NumericUpDown to be nullable
body: <p>I found this custom class that allows <code>NumericUpDown</code> to receive nullable values. I use it in an edit form which can be empty or could be populated with a data from a database from where the need for nullable values comes from.</p>
  
  <p>This is the current code :</p>
  
  <pre><code>public partial class NullableNumericUpDown : NumericUpDown
      {
          public NullableNumericUpDown()
          {
              InitializeComponent();
          }
  
          public NullableNumericUpDown(IContainer container)
          {
              container.Add(this);
  
              InitializeComponent();
          }
  
  
  
          private int? _value;
  
          [Bindable(true)]
          public new int? Value
          {
              get
              {
                  return _value;
              }
              set
              {
                  _value = value;
                  if (value != null)
                  {
                      base.Value = (int)value;
                      Text = Value.ToString();
                  }
                  else
                  {
                      Text = "";
                  }
              }
          }
  
          private void NullableNumericUpDown_ValueChanged(object sender, EventArgs e)
          {
              _value = (int)base.Value;
          }
  
  
  
          void NullableNumericUpDown_TextChanged(object sender, System.EventArgs e)
          {
              if (Text == "")
              {
                  _value = null;
              }
          }
      }
  </code></pre>
  
  <p>I'm fairly new to C# so I can't say I fully understand the code but however the problem that I have is when I populate the Form with data from the database and exactly when the <code>NullableNumericUpDown</code> has some value, let's say - <code>3</code>. When I change this value to say <code>5</code>, the final result when I collect the data from the filed in the form is <code>53</code>. Also if I delete <code>3</code> and then hit the incrementing arrow I get <code>4</code>. It seems that that the initial data is saved throughout the life cycle of the this control, I tried to set <code>0</code> at some current places I thought it might help, but it wouldn't and besides the fact that I need to get rid of this initial value if the value of the control is changed in fact it's not enough to just make it <code>0</code> cause if I have empty control this must be ok and record it as <code>null</code> instead <code>0</code>.</p>
  
  <p>Just to be complete here is how I set the data for the <code>NullableNumericUpDown</code> control :</p>
  
  <pre><code>numUpDnAreas.Value = entity.AreasCnt;
  </code></pre>
  
  <p>this happens on my <code>form_load</code> event. And when I click <code>Save</code> button I collect the data with this :</p>
  
  <pre><code>entity.AreasCnt = numUpDnAreas.Value;
  </code></pre>
  
  <p>Can this code be refactored to match my needs or should I leave it and just use something like <code>MaskedTextBox</code> or other?</p>