Creating labels or other objects at runtime 
Author Message
 Creating labels or other objects at runtime

I'm not very familiar with object oriented programming (although it's
impossible not to be confronted with it these days), and I would like to
know how I can create labels (or other objects) during runtime on my form,
without placing them there first in designing the form.
I understand I have to define (dim) them first, but how do I create them in
my code?


Thu, 01 Apr 2004 02:26:23 GMT  
 Creating labels or other objects at runtime
The easiest way to create conrols at runtime is to create a control array.
for a label you would place it on a form,
give it a name(in this case Label1) and set it's Index property to 0 and
it's visible property to False. then do something like this:

Private Sub Command1_Click()

    Dim i As Integer
    Dim mystring(4) As String

    mystring(0) = "These"
    mystring(1) = "are"
    mystring(2) = "dynamically"
    mystring(3) = "loaded"
    mystring(4) = "labels"

    Label1(0).Caption = mystring(0)
    Label1(0).Visible = True

    For i = 1 To 4
        Load Label1(i)
        With Label1(i)
            .Caption = mystring(i)
            .Top = Label1(i - 1).Top + Label1(i - 1).Height + 5
            .Left = Label1(i - 1).Left
            .Visible = True
        End With
    Next i

End Sub

Note that you have to place the controls dynamically or else VB will place
all the controls on top of each other. Also make sure to make th control
visible.


Quote:
> I'm not very familiar with object oriented programming (although it's
> impossible not to be confronted with it these days), and I would like to
> know how I can create labels (or other objects) during runtime on my form,
> without placing them there first in designing the form.
> I understand I have to define (dim) them first, but how do I create them
in
> my code?



Thu, 01 Apr 2004 06:07:56 GMT  
 Creating labels or other objects at runtime
just follow these steps:
1) create a label (lblMom) on your form
2) set its Index property to 0 so you can generate a control array
3) put the following code in your Form_Load Sub. here I create 3 more labels
based on lblMom and arrange them in a row at runtime

   i = 1
   Do While i <= 3         ' number of child controls to be created
      Load lblMom(i)
      lblMom(i).Left = i * lblMom(0).Width + lblMom(0).Left
      lblMom(i).Top = lblMom(0).Top
      lblMom(i).Visible = True
      i = i + 1
   Loop

cheers, abraham



Quote:
> I'm not very familiar with object oriented programming (although it's
> impossible not to be confronted with it these days), and I would like to
> know how I can create labels (or other objects) during runtime on my form,
> without placing them there first in designing the form.
> I understand I have to define (dim) them first, but how do I create them
in
> my code?



Thu, 01 Apr 2004 20:06:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Creating Textboxes , Labels and Other Objects At RunTime

2. Create a label at runtime

3. creating a label on a form in runtime

4. can you create labels during runtime?

5. Create label during runtime

6. Create a Label or a Textbox at Runtime in VB 5

7. Create TextBox & Label at runtime

8. Creating a Label Array or Collection at runtime

9. Creating Label Arrays at Runtime

10. Creating a label on at runtime

11. Create textboxes and labels on report at runtime

12. Create Label Object In Code

 

 
Powered by phpBB® Forum Software