Solved

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

Posted on 2004-09-10
10
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
10 Comments
 
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
        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 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")

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

726 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