Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Too early or too late, but I can't wrap my brain around this.

 

 

I want to do something like the following:

   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

  • *Experts*
Posted

You can do something like this:

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

  • *Experts*
Posted

Thanks Mutant,

 

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

 

AddHandler combo.selectedindexchanged, AddressOf comboIndexChangedHandler

 

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

 

 

Jon

  • *Experts*
Posted

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:

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:

AddHandler DirectCast(combo, ComboBox).SelectedIndexChanged, AddressOf handler

Posted
[

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

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...