void txtTradeInAllowance_Validating(object sender, CancelEventArgs e) { //declare a errorMessage string and set it's default value to empty string string errorMessage = string.Empty; //invoke the IsNumber in the AutomotiveManager class to check if the text of TradeInAllowance could be parse to a number if (!AutomotiveManager.IsNumber(txtTradeInAllowance.Text)) { //set error message errorMessage = "Please enter a numeric value."; } //parse the text of the TradeInAllowance textbox to decimal and check if it less than 0 else if (Decimal.Parse(txtTradeInAllowance.Text) < 0) { //set error message errorMessage = "Please enter a value greater than or equal to zero."; } //check if the errorMessage is not an empty string if (!errorMessage.Equals(string.Empty)) { //set the CancelEventArgs' Cancel property to true e.Cancel = true; //invoke SetError method in errorProvider control and set the errorMessage to TradeInAllowance textbox errorProvider.SetError(txtTradeInAllowance, errorMessage); //set focus to TradeInAllowance textbox txtTradeInAllowance.Focus(); } }
/// <summary> /// the event handler to check if the value of textbox is a numeric value /// </summary> void txtNumeric_Validating(object sender, CancelEventArgs e) { if (!AutomotiveManager.IsNumber(((TextBox)sender).Text.Trim())) { // Prevents the validated event from being triggered e.Cancel = true; if (!_errorFocus) { ((TextBox)sender).Focus(); _errorFocus = true; } // Show the error icon beside the control errorProvider.SetError(((Control)sender), "Please enter a numeric value for this field."); } }
/// <summary> /// the event handler to check if the value of textbox is a year value(4 digits) /// </summary> void txtYear_Validating(object sender, CancelEventArgs e) { if (!AutomotiveManager.IsNumber(txtYear.Text.Trim()) || txtYear.Text.Trim().Length != 4) { // Prevents the validated event from being triggered e.Cancel = true; if (!_errorFocus) { ((TextBox)sender).Focus(); _errorFocus = true; } // Show the error icon beside the control errorProvider.SetError(txtYear, "Please enter a valid four digit year. Eg. 1977"); } }
/// <summary> /// Cost textbox validating /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void txtCost_Validating(object sender, CancelEventArgs e) { string errorMessage = string.Empty; if (!AutomotiveManager.IsNumber(txtCost.Text)) { errorMessage = "Please enter a numeric value."; } else if (Decimal.Parse(txtCost.Text) < 0) { errorMessage = "Please enter a value greater than zero."; } if (!errorMessage.Equals(string.Empty)) { //set the CancelEventArgs' Cancel property to true e.Cancel = true; //invoke SetError method in errorProvider control and set the errorMessage to Cost textbox errorProvider.SetError(txtCost, errorMessage); //set focus to Cost textbox txtCost.Focus(); } }