Solved

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

Posted on 2004-09-10
10
597 Views
Last Modified: 2008-01-09
Hi,
  How can I create a array of combo boxes like I create in VB 6.0?
regards,
Badri.
0
Comment
Question by:gvsbnarayana
  • 4
  • 2
10 Comments
 
LVL 85

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
        ComboBoxes.Add(ComboBox1)
        ComboBoxes.Add(ComboBox2)
        ComboBoxes.Add(ComboBox3)
    End Sub

    Private Sub foo()
        Dim cb As ComboBox

        For Each cb In ComboBoxes
            ' do something with cb...
            Debug.WriteLine(cb.Name)
        Next
    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

Regards,

Idle_Mind
0
 
LVL 85

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")

Idle_Mind
0
 
LVL 8

Author Comment

by:gvsbnarayana
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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

Idle_Mind
0
 
LVL 8

Author Comment

by:gvsbnarayana
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?
regards,
badri.
0
 
LVL 18

Expert Comment

by:armoghan
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

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 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.

Idle_Mind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 30
Get Count in a variable with 5 digits always 1 27
Open a word document 23 33
How to use odbc in vb to connect to ms sql 14 26
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now