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

asked on

creating a new control

I've created my first custom control - a combobox.
Took a bit of digging to get it laid down right, but I think I have it now.
However, on my main form, I have a sub that is called by the containing comboboxes there.
I'd like to be able to call the same sub from my custom class.  I imagine I could just copy/paste it to the new class, but I'm curious if there's a more efficient way?

Also - my selectionchange event isn't firing...I must have missed something...  :(

Public Class CustomCombo
    Inherits ComboBox
    Public WithEvents CustomCombo As ComboBox
  
    Public Sub New()
        MyBase.Width = 145
    End Sub
 
  Private Sub CustomCombo_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomCombo.SelectionChangeCommitted
        CustomCombo.Items.Clear()
        CustomCombo.Visible = True
  End Sub
End Class

Open in new window

SOLUTION
Avatar of Bob Learned
Bob Learned
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
Avatar of sirbounty

ASKER

When I remove the WithEvents though, I get errors on this sub (the CustomCombo is underlined)


Private Sub CustomCombo_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomCombo.SelectionChangeCommitted
   CustomCombo.Items.Clear()
   CustomCombo.Visible = True
End Sub

Open in new window

I think that you misunderstood what I meant.  When you are creating a custom control, then you don't really need an internal control instance.  The class that you create is the control, and you should have that event handler outside of the class, on the form where you created the instance of the control.

Bob
Hmm - I suppose I still don't understand.  This is my first attempt at creating a custom class/control, so maybe that's why this is so difficult for me...I found the 'withevents' item on another site, and it worked, tho apparently it's not the right route to take.

I was actually taking advice on how to solve a dilema from a comment made at http:/Q_22987698.html#20366705

Basically my situation calls for a dynamically created 'main' combo to reference a partner combo.  As the main combo is selected, the secondary gets populated with items that reference what was chosen in the main combo.
My thoughts were to use the tag property, but from that thread, it seemed like the better route was to create a subclass and add additional properties.

If I'm going to create this subclass, I need someway of identifying that a selection was made...and then some way to point it to it's corresponding partner combo (though I'm still not convinced that this is even the right way to go...)
Just add a plain vanilla class to your project called CustomCombo and make it look like this:

    Public Class CustomCombo
        Inherits ComboBox

        Private Sub CustomCombo_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectionChangeCommitted

        End Sub
    End Class

See how it uses "Me" instead of CustomCombo?  Remember, you'll also need to add a property that will be used so this new control knows about it's partner, and knows whether it's the parent or the child.
Great - that eliminates the error.
Any ideas on associating the two?

In my New sub, I'd like to go ahead and populate the primary combo:

    Public Sub New()
        MyBase.Width = 145
        PopulatePrimaryCbo(Me)
    End Sub

But how to 'test' that it's not the secondary combo, since both will run the New() sub?
Dim cboPrimary, cboSecondary as CustomCombo

I can open a new question if this takes this one too far off course...
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
So, I set the Parent property like so:?

   Dim cboCat, cboSub As New CustomCombo
   cboCat.Parent = cboCat

If that's the case, I should remove the test for Parent from New(), and just run the public populate sub after setting the Parent property (I think)?
Dim cboCat As New CustomCombo()
Dim cboSub As New CustomCombo(cboCat)
Ah - I missed the optional signature...thanx!