Question Controls Added at Runtime.

macleodjb

Member
Joined
Jan 1, 2011
Messages
8
Programming Experience
Beginner
Hi Guys,

This is my first post here at vbdotnetforums, so forgive me if this is in the wrong area or something. I'm just starting out using vb.net and i am writing my first application. I have added some comboboxes at runtime to my form using the code below.

VB.NET:
    Private Sub AddCombobox(ByVal ControlName As String, ByVal Top As Integer, ByVal Left As Integer, ByVal IsSupp As Boolean, ByVal HighestNumber As Integer)
        Dim BoxValues As Integer = LottoClass.LowestNumber
        Dim NewCombo As New ComboBox
        NewCombo.Name = ControlName
        NewCombo.Width = ControlWidth
        NewCombo.Height = ControlHeight
        NewCombo.Top = Top
        NewCombo.Left = Left
        If IsSupp Then
            NewCombo.BackColor = Color.FromKnownColor(KnownColor.MistyRose)
        End If
        Do Until BoxValues = HighestNumber + 1
            NewCombo.Items.Add(BoxValues)
            BoxValues += 1
        Loop
        Me.HistGroup.Controls.Add(NewCombo)
        BoxValues = LottoClass.LowestNumber
    End Sub

Now my question is now that i have these controls added to the form, how can i iterate through them all and collect the values for sql injection. I have given each combobox a specific name when it was created such as Ball1, Ball2, Ball3.

I was hoping i could do something like this.
VB.NET:
ControlName = "Ball" & ctype(i,String)
ControlValue = ControlName.Selectedindex

Thanks for the help.
 
Declare a List(Of ComboBox) at the class level. You can then loop through that List to get access to the controls.

Alternatively, you can access them by name from the same Controls collection you added them to in the first place. That requires you to know the actual names though, which the List doesn't.
 
VB.NET:
Private list As New List(Of ComboBox)


 Sub Iterate_List()

        For Each mycombo As ComboBox In list
               With mycombo

                      ' 
                      '  here you process anything you need
                      ' 

               End With
        Next


End Sub
 
Back
Top