title: How to allow only numerical data in a textbox
body: <p>I'm a new C# student and I've been working on my project, however, I can't seem to get some of my textboxes to act properly.  I need to ensure that some of my textboxes receive only numerical data.  Currently, my medication, surgery, labs, and rehab textboxes return their messageBox message when I fill in the textbox with a numerical value which is opposite of what they are supposed to do.  </p>
  
  <pre><code>private void calculateButton_Click(object sender, EventArgs e)
          {
              int days;
              double medication;
              double surgery = 0;
              double labs = 0;
              double rehab = 0;
  
              if (int.TryParse(numDaysInHosp.Text, out days))
                  if (days &lt; 0)
                  {
                      MessageBox.Show("Days In Hospital must be greater than zero.");
                  }
  
              if (double.TryParse(medsCharges.Text, out medication))
                  if (medsCharges.Text != "")
                  {
                      MessageBox.Show("Charges for Medication cannot be blank.");
                      medsCharges.Focus();
                  }
  
              if (double.TryParse(surgCharges.Text, out surgery))
                 if (surgCharges.Text != "")
                 {
                    MessageBox.Show("Surgical Charges cannot be blank.");
                    surgCharges.Focus();
                 }
  
              if (double.TryParse(labFees.Text, out labs))
                 if (labFees.Text != "")
                 {
                      MessageBox.Show("Lab Fees cannot be blank.");
                      labFees.Focus();
                 }
  
              if (double.TryParse(rehabCharges.Text, out rehab))
                 if (rehabCharges.Text != "")
                 {
                      MessageBox.Show("Rehabilitation Charges cannot be blank.");
                      rehabCharges.Focus();
                 }
  
              { 
                  double dayChrgs = CalcDayChrgs(days);
                  double miscChrgs = CalcMiscChrgs(medication, surgery, labs, rehab);
                  double totlCost = CalcTotlChrgs(dayChrgs, miscChrgs);
  
                  stayChrgsLabel.Text = dayChrgs.ToString("c");
                  miscChrgsLabel.Text = miscChrgs.ToString("c");
                  totlCostLabel.Text = totlCost.ToString("c");
                  memberLevelLabel.Text = memberLevelLabel.ToString();
  
              }
          }
  </code></pre>
  
  <p>We are not allowed to use the keypress method to do this.  Additionally, I've tried using the following lines as well with negative results.  </p>
  
  <pre><code>(string.IsNullOrEmpty(TextBox.Text))
  if(TextBox.Text == "") 
  if(TextBox.Text.Length == 0)
  String.IsNullOrWhitespace()
  </code></pre>
  
  <p>What am I not seeing in my code?</p>