Control Collections

jfackler

Well-known member
Joined
Mar 15, 2003
Messages
393
Location
Cincinnati, Ohio
User Rank
*Experts*
Too early or too late, but I cant wrap my brain around this.


I want to do something like the following:
Code:
    Private Sub createcollection()
        Dim combocollection As ControlCollection

        Dim combo As Control

        For Each combo In Me.Controls
            If TypeOf combo Is ComboBox Then
                combocollection.Add(combo)

            End If
        Next
    End Sub

    Private Sub comboIndexChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles combocollection.selectedindexchanged
        fillthetotals()

    End Sub

I know I can add each of the coombobox controls to the "Handles" statement, but I have 56 of the comboboxes that I want to handle the same way. Messy code.

Thanks,

Jon
 
You can do something like this:
Code:
Private Sub createcollection()
        Dim combocollection As ControlCollection

        Dim combo As Control

        For Each combo In Me.Controls
            If TypeOf combo Is ComboBox Then
                combocollection.Add(combo)
                Addhandler combo.event, addressof comboIndexChangedHandler this will add this combobox
to the handler
            End If
        Next
    End Sub

    Private Sub comboIndexChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles combocollection.selectedindexchanged
        fillthetotals()

    End Sub
 
Thanks Mutant,

I want to handle the event .SelectedIndexChanged of the combobox.

Code:
AddHandler combo.selectedindexchanged, AddressOf comboIndexChangedHandler

intellisense tells me selectedindexchanged is not an event of System.Windows.Forms.Control.


Jon
 
You declare your combo box as control first. It would be better to declare you combo variable as a Combobox from the start. Something like this:
Code:
Dim combo as combobox
For Each combo In Me.Controls
     AddHandler combo.SelectedIndexChanged, AddressOf handler
Next
Or yif you want to keep it your way, you have to cast your control variable to the ComboBox type:
Code:
AddHandler DirectCast(combo, ComboBox).SelectedIndexChanged, AddressOf handler
 
Code:
[

Private Sub createcollection()

        Dim combocollection As ControlCollection

        Dim ctrl As Control
        Dim cbo as Combobox


        For Each ctrl In Me.Controls
            If TypeOf ctrl Is ComboBox Then
                 cbo = Ctype (ctrl, combobox)
                combocollection.Add(cbo)
                AddHandler cbo.event, AddressOf comboIndexChangedHandler this will add this combobox
to the handler
            End If
        Next
    End Sub

 
Private Sub comboIndexChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles combocollection.selectedindexchanged
        fillthetotals()

    End Sub
 
Back
Top