Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

vb.net 2005 - addhandler for dynamic controls

Posted on 2007-11-28
14
942 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:sirbounty
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 150 total points
ID: 20366080
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
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 150 total points
ID: 20366206
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
 
LVL 67

Author Comment

by:sirbounty
ID: 20366477
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 27

Expert Comment

by:VBRocks
ID: 20366507
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
 
LVL 67

Author Comment

by:sirbounty
ID: 20366566
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 20366630
"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
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 150 total points
ID: 20366631
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20366643
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20366685
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
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 150 total points
ID: 20366705
"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
 
LVL 67

Author Comment

by:sirbounty
ID: 20366766
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
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 150 total points
ID: 20366849
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
 
LVL 67

Author Comment

by:sirbounty
ID: 20372993
Thanx gang - I like the custom control idea, so I'm giving that a shot (http:/Q_22990271.html)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20375098
Also have opened this one - http://www.experts-exchange.com/Q_22990716.html, if anyone can help.

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question