Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake an ExtenderProvider to validate TextBoxes by using the LIKE statement in VB .NET
DescriptionThis example shows how to make an ExtenderProvider to validate TextBoxes by using the LIKE statement in VB .NET. The provider catches its clients' Validating events and verifies that the values satisfy a LIKE statement.
KeywordsLIKE, ExtenderProvider, validation, validate
CategoriesSoftware Engineering, Controls, VB.NET
 
See Tutorial: Introduction to ExtenderProviders in VB .NET for an overview of creating ExtenderProviders.

This provider stores LikePattern and LikeMessage values for each of its clients in its private LikeInfo class. It stores LikeInfo objects in its m_LikeInfo hashtable. It also adds an event handler to catch each client control's Validating event.

When that event fires, the provider's ValidateTextBox subroutine determines whether the client TextBox contains a value that satisfies the LIKE statement stored in LikePattern. If the TextBox doesn't satisfy the statement, it assigns an error to the TextBox using the provider's embedded ErrorProvider component.

The GetLikeInfo helper function returns a control's LikeInfo object or a default object it the control doesn't have an entry.

The AddOrRemoveIfNecessary helper routine compares a LikeInfo object to a default object. If the object has the default values, then the routine ensures that it is removed from the m_LikeInfo hashtable. If the object does not hold the default values, then the routine ensures that it is in the hashtable.

Finally, to help the program decide whether it is safe to exit, the provider includes a HasError function. This function validates all of the registered client controls to display errors for any that fail to satisfy the LIKE statement. It then moves focus to the one with the smallest TabIndex value and displays that control's error message in a message box.

 
Imports System.ComponentModel

<ToolboxBitmap(GetType(LikeProvider), "like_provider.bmp"), _
    _
 ProvideProperty("LikePattern", GetType(TextBox)), _
 ProvideProperty("LikeMessage", GetType(TextBox))> _
Public Class LikeProvider
    Inherits System.ComponentModel.Component
    Implements IExtenderProvider

... Component Designer generated code ...

    ' Information about a control's range.
    Private Class LikeInfo
        Public LikePattern As String
        Public LikeMessage As String

        Public Sub New()
            LikePattern = ""
            LikeMessage = ""
        End Sub

        ' Return True if this object represents no range.
        Public Function IsDefault() As Boolean
            Return (LikePattern.Length = 0) AndAlso _
                   (LikeMessage.Length = 0)
        End Function
    End Class

    ' The information about fields with ranges.
    Private m_LikeInfo As New Hashtable

    Private m_Enabled As Boolean = True
    <Category("Behavior"), _
     DefaultValue(True)> _
    Public Property Enabled() As Boolean
        Get
            Return m_Enabled
        End Get
        Set(ByVal Value As Boolean)
            m_Enabled = Value
        End Set
    End Property

    ' We can extend TextBoxes.
    Public Function CanExtend(ByVal client_control As _
        Object) As Boolean Implements _
        IExtenderProvider.CanExtend
        Return (TypeOf client_control Is TextBox)
    End Function

    ' Return this control's error message.
    <Category("Validation"), _
     DefaultValue("")> _
    Public Function GetLikePattern(ByVal client_control As _
        TextBox) As String
        Return GetLikeInfo(client_control).LikePattern
    End Function

    ' Set this control's error message.
    <Category("Validation"), _
     DefaultValue("")> _
    Public Sub SetLikePattern(ByVal client_control As _
        TextBox, ByVal error_message As String)
        ' Get this control's range info.
        Dim like_info As LikeInfo = _
            GetLikeInfo(client_control)

        ' Set the new minimum value.
        like_info.LikePattern = error_message

        ' Add or remove the LikeInfo if necessary.
        AddOrRemoveIfNecessary(client_control, like_info)
    End Sub

    ' Return this control's error message.
    <Category("Validation"), _
     DefaultValue("")> _
    Public Function GetLikeMessage(ByVal client_control As _
        TextBox) As String
        Return GetLikeInfo(client_control).LikeMessage
    End Function

    ' Set this control's error message.
    <Category("Validation"), _
     DefaultValue("")> _
    Public Sub SetLikeMessage(ByVal client_control As _
        TextBox, ByVal error_message As String)
        ' Get this control's range info.
        Dim like_info As LikeInfo = _
            GetLikeInfo(client_control)

        ' Set the new minimum value.
        like_info.LikeMessage = error_message

        ' Add or remove the LikeInfo if necessary.
        AddOrRemoveIfNecessary(client_control, like_info)
    End Sub

    ' A client is validating. See if it is blank.
    Private Sub Client_Validating(ByVal sender As Object, _
        ByVal e As System.ComponentModel.CancelEventArgs)
        If Not m_Enabled Then Exit Sub
        ValidateTextBox(DirectCast(sender, TextBox))
    End Sub

    ' Validate the TextBox.
    Private Sub ValidateTextBox(ByVal client_control As _
        TextBox)
        ' Get the control's text.
        Dim client_text As String = client_control.Text
        Dim like_info As LikeInfo = _
            GetLikeInfo(client_control)

        ' See if the text matches the pattern.
        If client_text.Length = 0 Then
            ' It's blank. Allow it.
            errBadMatch.SetError(client_control, "")
        Else
            ' See if it matches the LikePattern.
            If client_text Like like_info.LikePattern Then
                ' It matches.
                errBadMatch.SetError(client_control, "")
            Else
                ' It doesn't match.
                errBadMatch.SetError(client_control, _
                    like_info.LikeMessage)
            End If
        End If
    End Sub

    ' Return this control's LikeInfo.
    Private Function GetLikeInfo(ByVal client_control As _
        Control) As LikeInfo
        ' See if we have LikeInfo for this control.
        If m_LikeInfo.Contains(client_control) Then
            ' We have LikeInfo for this control. Return it.
            Return DirectCast(m_LikeInfo(client_control), _
                LikeInfo)
        Else
            ' We do not have LikeInfo for this control.
            ' Return a new default LikeInfo.
            Return New LikeInfo
        End If
    End Function

    ' Add or remove this LikeInfo if necessary.
    Private Sub AddOrRemoveIfNecessary(ByVal client_control _
        As Control, ByVal like_info As LikeInfo)
        ' See if the LikeInfo should be present but is not,
        ' or should not be present but is.
        If like_info.IsDefault <> Not _
            m_LikeInfo.Contains(client_control) Then
            If like_info.IsDefault Then
                ' The LikeInfo should not be present but is.
                m_LikeInfo.Remove(client_control)
                RemoveHandler client_control.Validating, _
                    AddressOf Client_Validating
            Else
                ' The LikeInfo should be present but is not.
                m_LikeInfo.Add(client_control, like_info)
                AddHandler client_control.Validating, _
                    AddressOf Client_Validating
            End If
        End If
    End Sub

    ' If some control has an error, display its error
    ' message,
    ' set focus to it, and return True. If all controls are
    ' okay, then return False.
    Public Function HasError() As Boolean
        If Not m_Enabled Then Return False

        ' Make sure all controls have been validated.
        Dim first_text_box As TextBox = Nothing
        Dim first_tab_index As Integer = Integer.MaxValue
        For Each text_box As TextBox In m_LikeInfo.Keys
            ' Validate the TextBox.
            ValidateTextBox(text_box)

            ' See if this is the first in the tab order so
            ' far.
            If errBadMatch.GetError(text_box).Length > 0 _
                Then
                If text_box.TabIndex < first_tab_index Then
                    first_text_box = text_box
                    first_tab_index = text_box.TabIndex
                End If
            End If
        Next text_box

        ' See if any control has an error.
        If Not (first_text_box Is Nothing) Then
            first_text_box.Focus()
            MessageBox.Show(errBadMatch.GetError(first_text_box))
            Return True
        End If

        Return False
    End Function
End Class
 
At design time, you set the properties for TextBoxes that you want to be required. The main program doesn't need to use any code to validate TextBoxes.

If you want to ensure that all TextBoxes are valid, call the HasError function in the form's Closing event handler to see if it is safe to close the form.

 
Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
    As System.ComponentModel.CancelEventArgs) Handles _
    MyBase.Closing
    e.Cancel = LikeProvider1.HasError()
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOk.Click
    Me.Close()
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCancel.Click
    LikeProvider1.Enabled = False
    Me.Close()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated