How to create array of Combo box control like we do in VB 6.0?

Posted on 2004-09-10
Medium Priority
Last Modified: 2008-01-09
  How can I create a array of combo boxes like I create in VB 6.0?
Question by:gvsbnarayana
  • 4
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12028632
That really depends on WHY you want the control array.

If you simply want to group them together so you can iterate through them then create an ArrayList and add them to it:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private ComboBoxes As New ArrayList

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub foo()
        Dim cb As ComboBox

        For Each cb In ComboBoxes
            ' do something with cb...
    End Sub

End Class

If you want a common event handling routine then you can use the Handles keyword to make one sub handle the events for more than one combobox like this:

    Private Sub AllComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged, ComboBox3.SelectedIndexChanged

        ' Notice that each combobox.selectedindexchanged that we are interested in is listed after the Handles keyword
        ' seperated by commas

    End Sub


LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12028659
I failed to mention that the sender parameter is the "equivalent" of the Index parameter in VB6.  It wil tell you which control fired the event.  You can cast it to a combobox with a statement like this:

    Dim cb As ComboBox = CType(sender, ComboBox)
    MsgBox(cb.Name & " SelectedIndexChanged")


Author Comment

ID: 12029101
I wanted to enter four records using the same forms. I have same validations for the four combo boxes and has done it using array of comboboxes in VB 6.0. Wanted to acheive the same feature in VB.net. Or is there any other way to do this?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12029374
Then you would use the second method I showed you where you add more than one control  after the Handles keyword:

    Private Sub Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating, ComboBox2.Validating, ComboBox3.Validating

    End Sub


Author Comment

ID: 12033652
Dear Idle_mind,
  This works fine if we wanted to just make some validation. But the structure is like this:
4 combo boxes, four text boxes, another found check boxes.
Upon selecting an item in one combobox, I will need to populate values in the corresponding text boxes.
Is this also possible? or how else I can achieve this result?
LVL 18

Expert Comment

ID: 12041402
Add a constant value in the Tag property of combo which shows the number of combo also add textboxes etc in arrayList.

Now on combo event check the Tag property and do the opporations with relavent ArrayList index textbox or other components

LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12049095
Here is a function that can return a control by name:

    Private Function getControlByName(ByVal controlName As String) As Control
        Dim i As Integer
        Dim ctl As Control

        controlName = controlName.ToLower
        For Each ctl In Me.Controls
            If ctl.Name.ToLower.Equals(controlName) Then
                Return ctl
            End If
        Next ctl
    End Function

So if you have ComboBox1, you can strip the 1 off the end (or store 1 in the tag as armoghan suggested) and then you can get a reference to the corresponding textbox and checkbox using the getControlByName() function I gave you above.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

862 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