Solved

How can I dynamically add selection values to my Combobox controls

Posted on 2013-06-03
1
304 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
[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
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39217447
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

717 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