Link to home
Start Free TrialLog in
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) ToutountzoglouFlag for Greece

asked on

Public Event in Public Function


Hi to all
I am trying to create a Function that Counts Controls with the same Tag
These controls are inside a ChildControl like GroupBox which they are Created in a parent Container like TabControl.TabPage
This Function should be call in a form load event
Then in this Function i need to add a TextChange Event handler Which detects how many of these controls with the same tag are different than an empty string (Not empty).
I will Post an example how it works in private
  For Each GroupBoxCtl In Me.TabControl1.TabPages(0).Controls
            If TypeOf GroupBoxCtl Is GroupBox Then
                For Each Xctl As Control In GroupBoxCtl.Controls
                    If Xctl.Tag = "Required" Then
                        AddHandler Xctl.TextChanged, AddressOf Xctl_TextChanged
                        x += 1
                    End If
                Next
            End If
        Next
        For Each GroupBoxCtl In Me.TabControl1.TabPages(0).Controls
            If TypeOf GroupBoxCtl Is TextBox Then
                If GroupBoxCtl.Tag = "Required" Then
                    AddHandler GroupBoxCtl.TextChanged, AddressOf Xctl_TextChanged
                    y += 1
                End If
            End If
        Next
    Dim x As Integer = 0
    Dim y As Integer = 0
 Private Sub Xctl_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim j As Integer = 0
        Dim k As Integer = 0
        For Each GroupBoxCtl In Me.TabControl1.TabPages(0).Controls
            If TypeOf GroupBoxCtl Is GroupBox Then
                For Each Xctl As Control In GroupBoxCtl.Controls
                    If Xctl.Tag = "Required" Then
                        If Xctl.Text <> "" Then
                            j += 1
                            Me.ContinueToNextTab2.Text = "There are  " & (x + y) - (j + k) & " Remaining Required Fields"
                        End If
                    End If
                Next
            End If
        Next
        For Each GroupBoxCtl In Me.TabControl1.TabPages(0).Controls
            If TypeOf GroupBoxCtl Is TextBox Then
                If GroupBoxCtl.Tag = "Required" Then
                    If GroupBoxCtl.text <> "" Then
                        k += 1
                        Me.ContinueToNextTab2.Text = "There are  " & (x + y) - (j + k) & " Remaining Required Fields"
                    End If
                End If
            End If
        Next
        If j + k = x + y Then
            Me.ContinueToNextTab2.Text = "You may proceed"
        Else
            Me.ContinueToNextTab2.Text = "There are  " & (x + y) - (j + k) & " Remaining Required Fields"
        End If
    End Sub

Open in new window

The above counts controls inside a tabpage and groupBoxes which are also in this tabpage

Now i need this as a Function to Public
Module ControlsFunctions

    Public Event TextChanged As CountControlsTagsEventHandler
    Public Function CountCertainControlsTag(ByVal ParentContainer As Control, ByVal ChildControl As Control, ByVal StringTag As String) As Integer
        Dim i As Integer = 0
        Dim ctl As Control
        For Each ctl In ParentContainer.Controls
            If ctl.[GetType]() Is GetType(GroupBox) Then
                For Each ChildControl In ctl.Controls
                    If ChildControl.Tag = StringTag Then
                        AddHandler ChildControl.TextChanged, New EventHandler(AddressOf ChildControl_TextChanged)
                        i += 1
                    End If
                Next
            End If
        Next

        Return i
    End Function
    Public Sub ChildControl_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent TextChanged() 'What do i have to reference here....
    End Sub
    Public Delegate Sub CountControlsTagsEventHandler(ByVal ParentContainer As Control, ByVal ChildControl As Control, ByVal StringTag As String)
End Module

Open in new window

How can i build the TextChange Event
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John (Yiannis) Toutountzoglou

ASKER

Much Easier...No more Comments...
Thank you Mike...