Which code segment should you use to ensure that the txtTN text box is validated?

You are creating aWindows Forms application for a courier company by using the .NET
Framework 3.5.You create a form that allows customers to track the progress of their
shipments. The form contains the following elements:
A text box named txtTN that allows users to enter a tracking number
An ErrorProvider control named ErrorProvider1 that informs users of an invalid
tracking number
A function named ValidTrackingNumber that validates tracking numbers

You need to ensure that the txtTN text box is validated. Which code segment should you use?

You are creating aWindows Forms application for a courier company by using the .NET
Framework 3.5.You create a form that allows customers to track the progress of their
shipments. The form contains the following elements:
A text box named txtTN that allows users to enter a tracking number
An ErrorProvider control named ErrorProvider1 that informs users of an invalid
tracking number
A function named ValidTrackingNumber that validates tracking numbers

You need to ensure that the txtTN text box is validated. Which code segment should you use?

A.
private void txtTN_Validating(object sender, CancelEventArgs e)
{
if (!ValidTrackingNumber(txtTN.Text))
{
errorProvider1.SetError(txtTN, “Invalid Tracking
Number”);
Cancel = true;
}
else
errorProvider1.SetError(txtTN, “”);
}

B.
private void txtTN_Validating(object sender, CancelEventArgs e)
{
if (!ValidTrackingNumber(txtTN.Text))
errorProvider1.SetError(txtTN, “Invalid Tracking
Number”);
else
{
errorProvider1.SetError(txtTN, “”);
Cancel = true;
}
}

C.
private void txtTN_Validated(object sender, EventArgs e)
{
if (!ValidTrackingNumber(txtTN.Text)) errorProvider1.SetError(txtTN, “Invalid Tracking
Number”);
else
{
errorProvider1.SetError(txtTN, “”);
txtTN.Focus();
}
}

D.
private void txtTN_Validated(object sender, EventArgs e)
{
if (!ValidTrackingNumber(txtTN.Text))
{
errorProvider1.SetError(txtTN, “Invalid Tracking
Number”);
txtTN.Focus();
}
else
errorProvider1.SetError(txtTN, “”);
}

Explanation:
protected void textBox1_Validating (object sender, System.ComponentModel.
CancelEventArgs e)
{
try
{
// Check to see if the TextBox contains a numeric value by trying
// to parse the string
double x = double.Parse(textBox1.Text);
errorProvider1.SetError(textBox1, “”);
}
catch (System.FormatException ex)
{
// If the text box does not contain a numeric value,
// set the error on the TextBox
errorProvider1.SetError(textBox1, “The value must be numeric”);
}
}

MCTS Self-Paced Training Kit (Exam 70-505): Microsoft.Net Framework 3.5-Windows Forms Application Development (pg. 581)



Leave a Reply 0

Your email address will not be published. Required fields are marked *