Remilia Scarlet Posted February 13, 2003 Ok, I just had a quick question. I can't find the answer to this myself. And I'm such an amature programmer, it may just be that I don't know what the Hell I'm doing. But... In Visual Basic, I'm wanting to create 1024 new labels. Now, I don't want to have to create every single one by hand, and reset each property by hand. Now, I've created my own classes in VB before, so I know how to do the "set Something = new SomeClass" thing. But, what I'm wanting to do is create new instances of labels in the code. It looks like I have to use the Set New keywords, as if I don't it'll give me an error that the block variable isn't set. But, I don't know what to set it to. Here's what I have so far: Private Sub Form_Load() Dim PlayGrid(1 To 1024) As Label Dim I As Integer For I = 1 To 1024 Set PlayGrid(I) = New [need help here] Next I For I = 1 To 1024 PlayGrid(I).Height = 5 PlayGrid(I).Width = 5 PlayGrid(I).BackColor = vbBlue PlayGrid(I).Left = I + (5 * I) PlayGrid(I).Top = I + (5 * I) Next I End Sub Any ideas? 0 Share this post Link to post
mewse Posted February 13, 2003 you're off your rocker.. creating controls at run-time in VB is actually really, really easy. you don't have to dim them, you just use a command called "load". create a form with one label named lblMewse. set it's "index" property to 0 to make it the first element of an array (with only one element, we will load the rest of the elements in dynamically). now, make a button called cmdSpawn that will spawn the new buttons. here is the code for the button: Public Sub cmdSpawn_Click() Dim i As Integer For i = 1 To 4 Load lblMewse(i) lblMewse(i).Top = lblMewse(i - 1).Top + lblMewse(i - 1).Height lblMewse.Visible = True Next i End Sub This is how you will be able to create labels at runtime. Note: controls created with "Load" begin with their visible property set to False, so you will have to set it to True for the label to show up. There is a companion to Load named (surprise) Unload, which will allow you to destroy labels that you have dynamically loaded in. You cannot use Unload on Controls that were placed at design time (ie. not created by Load). You cannot "Load" labels which already exist. Now, I hope all of this is actually what you are looking for, because you might just want to copy/paste blocks of the labels until you have 1000 :P 0 Share this post Link to post
Remilia Scarlet Posted February 13, 2003 Oh, ok...kind of like when I use Load to load and unload forms! heh, yeah...Thanks a lot! Now to do my experiment... 0 Share this post Link to post