by BlackCat (2 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 23rd September 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Field Validating It is useful for validating textbox, comboBox... when user input - using the Validating procedure - using a simple for loop
System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
Dim tBox As TextBox = DirectCast(sender, TextBox)
'Your validation code goes here.
If (tBox.Text = "ok") Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
========================================================
The following is a dynamic way to validate more than one fields
\\\
Dim last As String
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
last = DirectCast(sender, Control).Name
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).Text = last
End Sub
///
=======================================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CheckIfDirty(Me.Controls)
End Sub
Private Sub CheckIfDirty(ByVal ctrls As Control.ControlCollection)
For Each ctrl As Control In ctrls
If TypeOf ctrl Is TextBox Then
If DirectCast(ctrl, TextBox).Modified Then
Dim strOut As String
strOut = String.Format("{0} is dirty", DirectCast(ctrl, TextBox).Name)
Trace.WriteLine(strOut)
End If
End If
'check child controls if any
CheckIfDirty(ctrl.Controls)
Next
End Sub
==================================================
private void MyValidatingCode()
{
// Confirm there is text in the control.
if (textEmail.Text.Length == 0)
{
throw new Exception("Email address is a required field.");
}
// Confirm that there is a "." and an "@" in the e-mail address.
else if(textEmail.Text.IndexOf(".") == -1 || textEmail.Text.IndexOf("@") == -1)
{
throw new Exception("Email address must be valid e-mail address format." +
"\nFor example: '[email protected]'");
}
}
// Validate the data input by the user into textEmail.
private void textEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
MyValidatingCode();
}
catch(Exception ex)
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textEmail.Select(0, textEmail.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textEmail,ex.Message);
}
}
private void textEmail_Validated(Object sender, System.EventArgs e)
{
//If all conditions have been met, clear the error provider of errors.
errorProvider1.SetError(textEmail, "");
}
No comments have been posted about Field Validating It is useful for validating textbox, comboBox... when user input - using the Valid. Why not be the first to post a comment about Field Validating It is useful for validating textbox, comboBox... when user input - using the Valid.