Zebra0.com

csharp boolean

Getting Numbers from a Text Box

One of the reasons we use controls such as scroll bars, lists, and checkboxes is becaseu the user can not enter invalid values.

Sometimes, you may want to let the user enter a numeric value in a text box. The example below shows how to get and display salary from a text box.

private void txtSalary_TextChanged(object sender, EventArgs e)
{
   double salary;
   if (Double.TryParse(txtSalary.Text, out salary))
   {
      lblSalary.Text = String.Format("${0:0.00}", salary);
   }
   else lblSalary.Text = "That is not a valid number";
}

End of lesson, Next lesson: