Solved

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

Posted on 2004-09-10
10
584 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

6 Experts available now in Live!

Get 1:1 Help Now