Solved

How can I dynamically add selection values to my Combobox controls

Posted on 2013-06-03
1
299 Views
Last Modified: 2013-06-18
Hi Experts,
I use about 10 Combobox controls in my VB.Net application. The controls names all begin with "cboResult".  I want to loop through all my Combobox controls whose name begins with "cboResult" and add "Yes", "No", and "N/A" as possible selection values (i.e. Items.Add("YES"), etc...).  How can I do this?

Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Try something like...
Public Class Form1

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

    Private Sub FindCombos(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If ctl.Name.ToUpper.StartsWith("CBORESULT") AndAlso TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox = DirectCast(ctl, ComboBox)
                cb.Items.Add("YES")
                cb.Items.Add("NO")
                cb.Items.Add("N/A")
            ElseIf ctl.HasChildren Then
                FindCombos(ctl)
            End If
        Next
    End Sub

End Class

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

11 Experts available now in Live!

Get 1:1 Help Now