Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Remilia Scarlet

Question on something in VB

Recommended Posts

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?

Share this post

Link to post

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

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now