darakas Posted January 17, 2006 Posted January 17, 2006 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?? Quote
Cags Posted January 17, 2006 Posted January 17, 2006 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... 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
darakas Posted January 17, 2006 Author Posted January 17, 2006 :) 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. Quote
Cags Posted January 17, 2006 Posted January 17, 2006 It's a question I asked myself about 18months ago :) Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted January 18, 2006 Leaders Posted January 18, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.