Solved

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

Posted on 2001-06-20
5
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

626 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