Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add items to multiple comboboxes 1

Status
Not open for further replies.

diamondsc

Programmer
Jun 28, 2001
44
US
I am using Visual Studio 2005 and vb.net.

I have several comboboxes on a form. Each combobox needs to be filled with a list of items from a single datasource. Each list will consist of item descriptions from a different set of records from the datasource. I have accomplished this with a small subroutine. However, each combobox has to have its own subroutine because I have to use the name of the combobox. For example, this is the vb.net code I'm using to add an item to the combobox.

Me.ComboBox1.Items.Add(ItemDescription)

Is there some way to use Me.Controls.Item(ComboBoxName) to add the items? This way I could pass the name of the control to the subroutine and have all the comboboxes filled using just one subroutine. Or is there another way of doing this?

Thanks.
 
If you know the names of your controls at design time then the simplest method is to use Combobox1.items.add...

If your controls are being added at runtime you might want to try out the GetControlByName() method in faq796-5698

Or you could use an arraylist to hold an array of controls added at runtime and then use For Each ctrl as ComboBox in myCtrlArray...
 
Thanks techsmith. Your post made me realize I could pass the combobox name to the subroutine by declaring a parameter as ComboBox.

Public Sub FillCombo(ByVal ItemDesc as String, ByVal ComboBoxName as ComboBox)

My head wasn't on straight that day. Thanks again.
 
Public Sub FillCombo(ByVal ItemDesc as String, ByVal ComboBoxName as ComboBox)

ByVal will affect the "local" copy of the combobox. Instead use ByRef, if you want the REAL windows forms combobox to be affected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top