How do you make variable dim statments?

darakas

Newcomer
Joined
Jan 17, 2006
Messages
2
I know this must be one of those situations where there is an obvious answer but so far I have failed to see it.
Ok here’s the idea. Adding elements to a form programmatically, like a checkbox , based on reading some external data source. One check box for each entry.
How do you add a dynamically generated name to a dim statement?

Dim checkbox & I As New CheckBox
Where I is an incremented integer doesn’t work.

Dim name as string
Name= “chk” & I.tostring
Dim Name as new checkbox is also a never going to work

But there must be a better way than

I is incremented integer

Select I
Case 1 Dim checkbox1 & I As New CheckBox
Case 2 Dim checkbox2 & I As New CheckBox
Case 3 Dim checkbox3 & I As New CheckBox

Etc especial when u don’t know how many to allow for


Oh I know we have list box and other tools available but still would love to know how to get a variable string in to a Dim statement as a name ? Any thoughts??
 
I believe this is actually impossible. Why are you attempting to create objects of a specific name dynamically, since these objects are created dynamically you will never be accessing them by name anywhere else in your code. If you want to create a series of checkboxes based on a file you can just do...

Visual Basic:
        Dim i As Single

        For i = 0 To 10
            Dim tempCheckbox As CheckBox
            tempCheckbox.Name = "checkBox" & i.ToString()
            Me.Controls.Add(tempCheckbox)
        Next

Then to check a value in a checkbox you can loop through the Controls collection looking for a checkbox with the right name.
 
:)

See I knew there would be a obvioulsy easy way that i was missing. The object has a name propertie...DOH! Thanks Cags! Sorry for being so thick...one of those days i guess.
 
You may also want to research dynamic event handling (look up AddHandler and RemoveHandler) and consider storing dynamically created controls in some sort of array or specialized class so that you can access them in an organized and logical, yet dynamic, manner.
 
Back
Top