3/31/2023 0 Comments Winform errorprovider![]() ![]() Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.ISupportInitialize Public Class DXErrorProvider Public Sub ShowErrors(_errorMessages As IDictionary(Of String, String))įor Each _property As String In _errorMessages.Keysĭim _formControls As List(Of Control) = Me.Controls.Cast(Of Control).ToList()ĭim _formControl As Control = _formControls.FirstOrDefault(Function(_control) _control.Tag = _property)ĬustomerDetailErrorProvider.SetError(_formControl, _errorMessages(_property)) ''This will go through the error messages dictionary and SetError for any control which are have error message _errorMessages.Add(_controlToValidate.Tag, _controlToValidate.Tag & " is required.") If Not _errorMessages.ContainsKey(_controlToValidate.Tag) Then _errorMessages.Remove(_controlToValidate.Tag) If Not _textBox Is Nothing AndAlso Not String.IsNullOrEmpty(_textBox.Text) Then Private Sub ValidateSingleControl(_controlToValidate As Control)ĭim _textBox As TextBox = IIf(_controlToValidate.GetType().IsAssignableFrom(GetType(TextBox)), DirectCast(_controlToValidate, TextBox), Nothing) ValidateSingleControl(_controlToValidate) Private Sub ValidateAllControls(ByVal _controlsToValidate As List(Of Control))įor Each _controlToValidate As Control In _controlsToValidate Private Sub Controls_Validating(sender As Object, e As EventArgs)ĭim _currentControl As Control = DirectCast(sender, Control) ''This event will be called for FirstName and LastName Textbox when the controls lost focus ''Attaching both textboxes to the 'Validating' event with method 'Controls_Validating'ĪddHandler txtFirstName.Validating, AddressOf Controls_ValidatingĪddHandler txtLastName.Validating, AddressOf Controls_Validating ''To fill all controls to be validated when submit button clicked _controlsToBeValidated = New List(Of Control) ''To hold all controls to be validated when submit button clicked _errorMessages = New Dictionary(Of String, String) ''Each controls tag property should be having some unique value ![]() Key is controls Tag property, Value is error message ''Dictionary to hold error message for any control. ReadOnly _controlsToBeValidated As List(Of Control) = Nothing ReadOnly _errorMessages As Dictionary(Of String, String) = Nothing Hope fully this below example will help you. ShowErrors() method will be going through each error messages in the dictionary, then find the control which matches the value that has in the control’s tag property, then SetError to that control. So, that it can be set during ShowError method call. (txtFirstName has “FirstName”, txtLastName has “LastName”) This tag will be used to identify each control, also the same tag value will be used to set error message in to the dictionary. In my example, I have used to hold some unique value for each control. This can be used to set some value which can be used programmatically. One other thing to be noted is, each control has one common property call “Tag”. If the control is valid, then it will remove the error message from the dictionary when there is any error. If the control is not valid, then it will add error message to the dictionary. Then, that control will be validated by the “ValidateSingleControl” method. ControlsValidating will get the control which raised the ‘Validating’ event, as a sender parameter. In my example, AttachHandlers method will attach event handler (Controls Validating) for both FirstName and LastName textboxes. So, whenever any control attached to that event lost focus, then the validation method will be called. i.e I have attached single validation method for multiple controls. So, I have used a single validation method to validate. But that is really going to be looking ugly when we have 15 or 20 textboxes or controls to be validated on a single form. You can have validating event for each text box. This event will be raised whenever the text box lost focus. If we need to display error message whenever the textbox lost focus, then you may want to use “Validating” event that comes with the controls. This example will show error messages whenever the textboxes lost focus, or whenever the Submit button clicked. txtFirstName, txtLastName, btnSubmit and CustomerDetailErrorProvider. CustomerDetailErrorProvider.SetError(txtFirstName, “First name is required”) įollowing example has two textboxes, button and an error provider control. Error provider will accept the field to which the error message should be displayed also, the error message which needs to be displayed. As you said Error providers can show error messages for the controls which are having seterror. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |