Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

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
SOLUTION
Avatar of Joel Coehoorn
Joel Coehoorn
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
SOLUTION
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 sirbounty

ASKER

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)...
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

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...
ASKER CERTIFIED SOLUTION
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
SOLUTION
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
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

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...
SOLUTION
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
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.
SOLUTION
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
Thanx gang - I like the custom control idea, so I'm giving that a shot (http:/Q_22990271.html)
Also have opened this one - https://www.experts-exchange.com/questions/22990716/addhandler-variables.html, if anyone can help.

<edit> - nevermind, I deleted it....