Solved

Creating objects on a form using Form_Load.....

Posted on 2001-06-20
5
161 Views
Last Modified: 2010-05-02
Ok, this one is a bit harder.

I want 31 new labels for my form, with the values 1 - 31. I know there must be an easier way to add them other then manually enter them in, so I thought I would ask the experts!!

I appreciate any help

Lewis Keen
a.k.a Yeggstry
0
Comment
Question by:yeggstry
5 Comments
 
LVL 4

Expert Comment

by:beckingh
ID: 6211887
At design time, no.  

The only way to do it is to add them manually.  You could create them at run time using Form1.Controls.Add but that would require resizing, locating, and setting all other properties at run time (which is a pain).
0
 
LVL 2

Accepted Solution

by:
chandukb earned 200 total points
ID: 6211909

Here is the sample Code:  First put one label control on the form in the design time, then, call this procedure with number of labels u want.

Private Sub LoadPOControls(iLoadCount As Integer)
'dynamically load controls

On Error GoTo ErrHandler

Dim iUnloadCount As Integer

If iLoadCount > 0 Then
   
    'Load Controls
    lblLabel(0).Caption = 1
    For lcv = 1 To iLoadCount - 1
        With frm
            Load .lblLabel(lcv)
            .lblLabel(lcv).Top = .lblLabel(lcv - 1).Top + 400
            .lblLabel(lcv).Caption = Val(.lblLabel(lcv - 1).Caption) + 1
            .lblLabel(lcv).Visible = True
        End With
    Next

End If

End Sub


Delete the duplication queston on this.

Chandu
0
 

Expert Comment

by:nomulap
ID: 6212011
Place no controls on the form and use this code in the form load event

Private Sub Form_Load()
Dim i As Integer
Dim ControlX As Control
   i = 1
   For i = 1 To 31
      Me.Controls.Add "VB.Label", "Label" & i, Me
      For Each ControlX In Me.Controls
         If ControlX.Name = "Label" & i Then
            ControlX.Visible = True
            ControlX.Top = i * 400
            ControlX.Height = 300
            ControlX.Width = 1000
            ControlX.Caption = ControlX.Name
            ControlX.BorderStyle = 1
         End If
      Next
   Next

End Sub
0
 
LVL 4

Expert Comment

by:amit_panjwani
ID: 6215129
why dont you try using control Array ?

Although , if you use control array you trap events but if you plan to create controls at run time  using Withevents ( withevents has limitations viz in VB6 ie is it doesnt allow arrays) .

Moreoever if you simply replicate controls during runtime with out using withevents - you cannot trap events.


Only thing you can do is to use control array

If you need more info on how to do it , lemme know.


Amit
0
 

Expert Comment

by:costello
ID: 6400355
Rejecting nomulap's answer.

Force accepting chandukb's comment.

costello
Community Support Moderator @ Experts-Exchange
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question