*Experts* jfackler Posted June 24, 2003 *Experts* Posted June 24, 2003 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 Quote
*Experts* mutant Posted June 24, 2003 *Experts* Posted June 24, 2003 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 Quote
*Experts* jfackler Posted June 24, 2003 Author *Experts* Posted June 24, 2003 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 Quote
*Experts* mutant Posted June 24, 2003 *Experts* Posted June 24, 2003 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 Quote
Heiko Posted June 24, 2003 Posted June 24, 2003 [ 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 Quote .nerd
*Experts* jfackler Posted June 24, 2003 Author *Experts* Posted June 24, 2003 (edited) You the man.....men. Thanks to you both, Jon Edited June 24, 2003 by jfackler Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.