Reusing the same code over and over

blacksaibot

Member
Joined
Mar 22, 2012
Messages
8
Programming Experience
Beginner
I have a bunch of forms in my project that will be using the same layout of comboboxes (cbox)
Cbox1's selected item affects the choices within Cbox2, and Cbox2's selected item affects the choices within Cbox3 (and so on).

So... on the top of 90% of the forms I'm creating will have the same exact comboboxes.

Is there a way I can create these controls and their code ONCE, and just reuse it on each different form?

But the thing is, more than one form might be open, and how can I make sure that when I select choices on one form, they won't translate to the other form open?
 
Can I make some kind of "class" called MyCombobox1 and give it some code and then reuse MyCombobox1 over and over on my many forms?
 
You're on the right track. You would add a new UserControl to your project. You add it in basically the same way as you add a form, just selecting the different option. You then design and build the UC in exactly the same way as a form, i.e. drag controls from the Toolbox onto the design surface, configure them in the Properties window and then add event handlers and other members in code. Once you build your project, your UC will appear in the Toolbox and you can add it to your forms just as you would any other control.

Because you're adding the UC to your forms, you want to be able to deal with just members of the UC in your form's code, not members of its child controls. That means defining a lot of pass-through members in your UC to expose the corresponding members of your ComboBoxes. For instance, here's how you could expose the DataSource property and SelectedIndexChanged event of ComboBox1 outside the UC:
Public Class UserControl1

    Public Property DataSource1 As Object
        Get
            Return ComboBox1.DataSource
        End Get
        Set(value As Object)
            ComboBox1.DataSource = value
        End Set
    End Property

    Public Event SelectedIndex1Changed As EventHandler

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        RaiseEvent SelectedIndex1Changed(Me, EventArgs.Empty)
    End Sub

End Class
Now, whenever you set the DataSource1 property of the UC it will set the DataSource property of ComboBox1. Likewise, whenever ComboBox1 raises its SelectedIndexChanged event the UC will raise its SelectedIndex1Changed event. You'll need to do the same sort of thing for all members of each ComboBox that you need to access from outside the UC. Note also that you should change the access level of the ComboBoxes from the default Friend to Private, so that they cannot be accessed at all from outside.
 
I'm really not following this quite well.

I just want to create a combobox called cbx_pc and it's datasource on every form where I add it will always be cbx_pc.DataSource = getItemsForCbox("SELECT PC FROM PrimeContracts") which should never, ever change.

That's all.
 
Post #1 seems to suggest that you have a group of three ComboBoxes that you want to repeat several times throughout the app. Post #4 seems to contradict that, saying it's not a group of three, but only one ComboBox. We can't help you solve a problem if you can't clearly describe that problem to us.
 
It depends on the form. Some will use combobox1, 2, and 3. Others will use only 1 and 2.
So I just want to start small and learn how to make one combobox that can be used over and over. It doesn't matter if it's a group or not.

So let's start with that. I want to make a combobox to use over and over again. I'll worry about the rest later.
 
It's not really that simple. If you want to make a custom ComboBox then, as with any other type, you create your own class and simply inherit the ComboBox class. That's all that happens when you add a Form or UserControl to your project: it adds a class and inherits the Form or UserControl class. You can then add whatever custom code you want, e.g. a constructor that sets the DataSource. Architecturally though, it would be very wrong to have a control that contains data access code to set its own DataSource. Also, if you do create a UserControl for multiple ComboBoxes then it would be more correct, although still wrong, to use standard ComboBoxes and put the extra code in the UserControl.
 
Then I guess I'll do what I've always done. Copy and paste the controls and their code over and over again. And if the database ever changes, I get to go through all the forms and adjust each and every line of code to reflect the database changes......

That sounds so bad. There should be a way where I make one change in one place, and then all the comboboxes that share that code are affected.
 
And there is such a way. Write one method that does the work and put it somewhere accessible to all forms. That method could take a ComboBox as an argument and set its DataSource. You could then call that method anywhere you have a ComboBox that needs its DataSource set and pass that ComboBox. If that functionality ever has to change then you only have to change that one method.
 
yes...write a method for it....

public sub generateComboBox(byref Combo as Combobox)

'do work here...set all the properties here as well

end sub

InkedGFX
 
yes...write a method for it....

public sub generateComboBox(byref Combo as Combobox)

'do work here...set all the properties here as well

end sub

InkedGFX

There's no reason to declare that parameter ByRef. In VB.NET you should be using ByVal in all cases except those where you specifically need to use ByRef. Those exceptions would be when you want to assign a value to the parameter inside the method and have that change affect the original and, for value types only, when you want to set a field or property of the parameter inside the method and have that change affect the original.
 
Back
Top