AllTheTimeWorld.com

csharp booleanGetting Numbers from Text Box

Getting Numbers from Text Box

Numeric values in a text box must be parsed.

Getting Numbers from Text Box

Numeric values in a text box must be parsed.

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";
}