Creating Controls At Runtime using a Variable Name

JCreationsInc

Active member
Joined
May 2, 2003
Messages
42
Location
San Jose Ca (Silicon Valley)
I am making a skin program that will allow a user to preview his/her progress on a form created programatically. Well what I was wondering is if any knows a way of creating the controls for that form using a string for the name??

Code:
Dim ProgramName as String

ProgramName = "PoopList"

Dim ProgramName as New ComboBox
ProgramName.Show

Anybody got any ideas??
 
JCreationsInc

Here is something to get you started:

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call ComboCreator()
    End Sub

    Private Sub ComboCreator()
        Dim x As Integer
        Dim sProgramName As String = "PoopList"
        For x = 1 To 10
            Dim cmbProgramName As New ComboBox()
            cmbProgramName.Top = x * 20
            cmbProgramName.Name = sProgramName & x

            Me.Controls.Add(cmbProgramName)
        Next
    End Sub

Create a new form, add a button to it and then put the above code in.

georg
 
And if you use what georg showed you and you want to access them later with code use this:
Code:
Dim ctrl As Control
For Each ctrl In Me.Controls
   If TypeOf ctrl Is ComboBox
        If ctrl.Name = "Combo1" or whatever number and name you set to it
               Do something to the combobox
        End If
   End If
Next
 
Not exactly :-)

You need to program a recursion, I think.

me.controls only contains the "top level" controls. More often than not these are GroupBoxes and panels.
 
Oh, you mean tab controls. It wont contain the tabs as they belong to the tabcontrol, not the form. I thought you meant that you need recursion for every control thats not a container. You only need recursion for those that have their own child controls.
 
Every control can be a container, so youd want to check every controls Controls collection.
 
woa nelly

I should check my posted threads more often. I found the solution right after posting this thread here is what I used...

Code:
In the load event of the form:
Dim DynamicCombo As New ComboBox()

Set the properties and display the control
DynamicCombo.Name = "PoopList"
DynamicCombo.DropDownStyle = ComboBoxStyle.DropDownList
DynamicCombo.Items.Add("First List Item")
DynamicCombo.Items.Add("Second List Item")
DynamicCombo.Items.Add("Third List Item")
DynamicCombo.SelectedIndex = 0
Me.Controls.Add(DynamicCombo)
DynamicCombo.Show()

This code is in the skin drawing procedure of my class:
Loop through all the controls on the window and get our control
Dim OurCombo As Integer, ComboName As ComboBox
For OurCombo = 0 To ControlCount - 1
    If Window.Controls.Item(OurCombo).Name = "PoopList" Then
        ComboName = Window.Controls.Item(OurCombo)
    End If
Next

Get the combobox Properties
ComboName.Left = "10"
ComboName.Top = "10"
ComboName.Width = "100"
ComboName.BackColor = color.black
ComboName.ForeColor = color.white
ComboName.Font = font.arial

It seems like it is working pretty good. Thank you anyways for your help you dudes are groovy baby yeah!
 
Back
Top