?
Solved

creating a new control

Posted on 2007-11-29
10
Medium Priority
?
182 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:sirbounty
[X]
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
  • 5
  • 3
  • 2
10 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 400 total points
ID: 20373378
The interesting thing is that you have an instance of another control within your custom control.  

You only need this:
Public Class CustomCombo
    Inherits ComboBox
  
    Public Sub New()
        MyBase.Width = 145
    End Sub
 
End Class

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 20373397
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20374138
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
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 67

Author Comment

by:sirbounty
ID: 20374367
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...)
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20375749
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.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20375900
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...
0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 1000 total points
ID: 20376266
I'd have something like this in the class:

    Private ParentBox As CustomCombo = Nothing

    Public Sub New(Optional ByRef Parent As CustomCombo = Nothing)
        MyBase.Width = 145
   
        If Parent Is Nothing Then
             PopulatePrimaryCbo(Me)
        Else
             ParentBox = Parent
        End If
    End Sub

Then if ParentBox is nothing, you know you are the parent.  Otherwise you have a reference to the parent already handy.  Also, you may want to make PopulatePrimaryCbo() a member of the new control.  Just have a public method called 'Populate()' that you can call from your main code, and perhaps pass a database connection to if needed.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20376389
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)?
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20376407
Dim cboCat As New CustomCombo()
Dim cboSub As New CustomCombo(cboCat)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20376467
Ah - I missed the optional signature...thanx!
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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