Download presentation
Presentation is loading. Please wait.
Published byBerniece Peters Modified over 9 years ago
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.