vb.net 2005 - addhandler for dynamic controls

Okay, I have two comboboxes - clicking the first runs code to populate the 2nd.

Now, I've recently adjusted my code to basically duplicate those combo boxes at run time.

I think I've got the addhandler right:

AddHandler cbo.SelectionChangeCommitted, AddressOf cboCat_SelectionChangeCommitted

But how can I alter the sub so that it accepts any caller and updates the callers related combo?

Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCat.SelectionChangeCommitted
    cboCatDetails.Items.Clear()
    PopulateDetails() 'Fairly sure I'll just need to pass the controls name as a combobox to this sub...
    cboCatDetails.Visible = True
End Sub
LVL 67
sirbountyAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
"I'm not opposed to updating tag properties when these controls are created"

If you are creating "pairs" of ComboBoxes then this is the easiest method.  Just store the second ComboBox in the Tag of the first.

    Dim cb1 As New ComboBox
    Dim cb2 As New ComboBox
    cb1.Tag = cb2

Use code like you've been given already to cast the sender to a ComboBox:

    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox)

Then cast the Tag as well:

    Dim cboPartner As ComboBox = DirectCast(cboCaller.Tag, ComboBox)
0
 
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCat.SelectionChangeCommitted
    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox)
    cboCaller.Items.Clear()
    PopulateDetails() 'Alter this sub if needed in a manner similar to what you see here
    cboCaller.Visible = True
End Sub
0
 
VBRocksConnect With a Mentor Commented:
Also, alter your PopulateDetails sub to take a ComboBox as an argument:

    Private Sub PopulateDetails(ByVal cbo As ComboBox)

        Debug.Print(cbo.Name)

    End Sub


And then, using jcoehoorn's example above, pass the combobox to the sub:
PopulateDetails(cboCaller)

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sirbountyAuthor Commented:
Hmm - ok, but how do I reference the 'other' combo?

When cbo1 is clicked, I need to populate cbo2, not clear cbo1 (which is what this code does)...
0
 
VBRocksCommented:
You can add a test, for example:

Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCat.SelectionChangeCommitted
    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox)
    If cboCaller IS cboCatDetails then
        cboCaller.Items.Clear()
    End If

    PopulateDetails() 'Alter this sub if needed in a manner similar to what you see here
    cboCaller.Visible = True

End Sub

0
 
sirbountyAuthor Commented:
Hmm - I still don't get it...there should be some way to identify 'both' combos, right?

So, in a cboSelectionChanged event, it should still be able to reference cbo2 as well...

Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCat.SelectionChangeCommitted
    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox) 'this would only be the primary combo.  I won't be calling this sub with the secondary combo, but I need some way to 'tie' them together...
    PopulateDetails('some reference to cboCaller's "partner" is what I need here...)

I'm not opposed to updating tag properties when these controls are created, unless there's a simpler way...
0
 
VBRocksConnect With a Mentor Commented:
Well, if both (or all) comboboxes are using the same event handler, then when the event for each
combobox is fired, the same sub is going to be called.  The Sender argument is going to be different
depending on which combobox fired the event.

For example, create 2 comboboxes, and assign the same event:
Dim cbo1 as New ComboBox()
Dim cbo2 as New ComboBox()
Me.Controls.Add(cbo1)
Me.Controls.Add(cbo2)


Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As _
    System.EventArgs)

    'Here, "sender" is going to be whichever combobox raised the event
    Dim cbo as ComboBox = sender

    'If you want to perform certain actions based off of which combobox raised the event, then
    '    you need to test to see which combobox it is:
    If cbo Is me.cbo1 Then
        cbo.Items.Clear()
    ElseIf cbo Is Me.cbo2 Then
        'Do something else...
    End If

    'Since these lines of code are not part of the test above, then they will be executed every time
    '    this event is fired, regardless of which combobox fired the event (in this case, both comboboxes):
    PopulateDetails()
    cbo.Visible = True

End Sub



0
 
VBRocksCommented:
Oops, I forgot to demonstrate adding the event handler for both combos:

Dim cbo1 as New ComboBox()
Dim cbo2 as New ComboBox()
Me.Controls.Add(cbo1)
Me.Controls.Add(cbo2)

AddHandler cbo1.SelectionChangeCommitted, AddressOf cboCat_SelectionChangeCommitted
AddHandler cbo2.SelectionChangeCommitted, AddressOf cboCat_SelectionChangeCommitted

Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As _
    System.EventArgs)

    'Here, "sender" is going to be whichever combobox raised the event
    Dim cbo as ComboBox = sender

    'If you want to perform certain actions based off of which combobox raised the event, then
    '    you need to test to see which combobox it is:
    If cbo Is me.cbo1 Then
        cbo.Items.Clear()
    ElseIf cbo Is Me.cbo2 Then
        'Do something else...
    End If

    'Since these lines of code are not part of the test above, then they will be executed every time
    '    this event is fired, regardless of which combobox fired the event (in this case, both comboboxes):
    PopulateDetails()
    cbo.Visible = True

End Sub

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
VBRocks, only the "primary" combo calls the sub...see the authors remarks in his last post:

    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox) 'this would only be the primary combo.  I won't be calling this sub with the secondary combo, but I need some way to 'tie' them together...
0
 
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
"Hmm - I still don't get it...there should be some way to identify 'both' combos, right?"

Not really.  An event handler gives you access to the object that raised the event (the 'sender') and the event arguments.  So for your combobox handler you get a reference to the comboxbox that raised the event and the standard combobox event arguments, which doesn't really have much new information.  Of course, you also have access to class members, so you could find the correct control that way, but that can be slow.

It looks like you really have two different things going on for each combox, and so maybe you should have separate methods to be the events handlers for each combobox.  But that still doesn't solve your need to know about about both comboxboxes in one event handler.  In that case, using the Tag property is probably the simplest way.  

Some people don't like using the Tag property because it doesn't scale well beyond one extra property, and they say it makes your code less clear, because references to SomeObject.Tag could mean anything.  If you or anyone who might see your code is in that camp you might want to create an new custom control that inherits from combobox or a new user control with two comboboxes.  Then you can add whatever extra properties you need and using them will result in readable code.  Plus, you can handle SelectionChanged event there with the custom control rather than needing to use AddHandler.
0
 
sirbountyAuthor Commented:
Okay this is starting to make more sense to me - thanx for the explanations.

Basically my routine is this:

Primary Combo is populated in form load (sqlreader).
When a selection is made from that combo, the secondary is poplated with a subset of the selection chosen in the primary combo.

I may then also need to run addtiional code based upone what was chosen in the secondary, so perhaps both combo selections will trigger the same event?  (I think I was mislead by the 'handles' clause at the end of the sub - I assumed that was the 'only' control being 'handled'.

And it sounds like I can either have a condition in the one (if cboCaller is primary - do this, else do that) or I can set up two event handlers (addhandler blah addressof cboPrimarySelect, and then addressof cboSecondarySelect).

Does that sum it up in a nutshell?  Hope I'm getting this anyway...

Tag property doesn't bother me much, but I think creating a custom control is stretching my skills.  If it's truly the better route to go, then I can certainly open a new post on it, cause I would need some help with it for sure.
0
 
VBRocksConnect With a Mentor Commented:
The 'handles' clause appear at the end of the sub, because in the form designer, you double-clicked
on the event for that combobox, and VB.NET automatically "wired" it up for you, (or you selected
it from the drop-down control event list).

However, when you manually add the event handler to a control using the AddHandler statement,
then it will use the same event handler as the other combobox (if that is what you specified), even
though the event doesn't have a 'handles' clause for that combobox after it.


For example, both comboboxes here (cbo1 and  cboCat) will use the same event:

Dim cbo1 as New ComboBox()
Me.Controls.Add(cbo1)
AddHandler cbo1.SelectionChangeCommitted, AddressOf cboCat_SelectionChangeCommitted


Private Sub cboCat_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCat.SelectionChangeCommitted
    Dim cboCaller As ComboBox = DirectCast(sender, ComboBox)
    If cboCaller IS cboCatDetails then
        cboCaller.Items.Clear()
    End If

    PopulateDetails() 'Alter this sub if needed in a manner similar to what you see here
    cboCaller.Visible = True

End Sub

0
 
sirbountyAuthor Commented:
Thanx gang - I like the custom control idea, so I'm giving that a shot (http:/Q_22990271.html)
0
 
sirbountyAuthor Commented:
Also have opened this one - http://www.experts-exchange.com/Q_22990716.html, if anyone can help.

<edit> - nevermind, I deleted it....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.