1 / 6

C# - FCL/Form & Control Validation

C# - FCL/Form & Control Validation. John Kelleher. Validating Controls. Ensure correct data is input Validating event Raised when control is asked to validate contents Programmer writes event handler If contents found to be invalid programmer can notify user

Download Presentation

C# - FCL/Form & Control Validation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# - FCL/Form & Control Validation John Kelleher

  2. Validating Controls • Ensure correct data is input • Validating event • Raised when control is asked to validate contents • Programmer writes event handler • If contents found to be invalid programmer can notify user • If contents found to be valid, Validated event is raised and focus is passed to next control

  3. Validating Event • Only raised… • if control has CausesValidation property set to true …and • when focus is passed to next control whose CausesValidation property set to true • Programmer write response: e.g. inform user of error

  4. Validating Event (contd.) • Event handler is of type CancelEventHandler • this passes CancelEventArgs object • Event handler sets Cancel flag to true for the CancelEventArgs object if data is invalid • Focus does not pass to next control! • Event handler sets Cancel flag to false for the CancelEventArgs object if data is valid • Validated event is raised

  5. Example private ErrorProvider errorProvider = new ErrorProvider(); … Private void textbox_Validating(object sender, CancelEventArgs e) { string errText = “”; if (textbox.Text.Length == 0) { e.Cancel = true; errText = “This field must not be empty”; } errorProvider.SetError(textbox, errText); }

  6. Code explanation • Basic error messaging system • If control validation fails, focus remains with control • Send empty string to errorProvider to clear error message/status • Ensure CausesValidation property for Cancel button is set to false • This ensures that focus can pass to the button and the user may cancel the form

More Related