Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 56
MS Access - Capture pressed key onclick 4 29
vb6 connector to SQL Server 2 34
using web browser with BING 40 111
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

864 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

19 Experts available now in Live!

Get 1:1 Help Now