Solved

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

Posted on 2004-09-10
10
608 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL syntax in VB.net 5 39
Close form "before" open 3 40
Help with converting xml file to excel using VB.NET 3 20
VB.NET 2008 Winforms Signing 13 19
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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