Solved

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

Posted on 2001-06-20
5
159 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now