Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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.
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…
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…

740 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