Solved

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

Posted on 2001-06-20
9
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
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6211913
Dim lbl As Label
Dim cnt As Integer

LastTop = 0
For cnt = 1 to 31
  Set lbl = Me.Controls.Add("VB.Label", "lbl" & cnt)
  With lbl
    .Move 10, cnt
    .AutoSize = True
    .Visible = True
    .Caption = cnt
  End With
Next cnt


try that maybe?
0
 

Author Comment

by:yeggstry
ID: 6211923
cheers, i will try, but any other solutions are welcome!!
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6211933
also you can add one label to your form, name it lblNumbers and set its index property to 0. then:

Dim cnt as Integer

for cnt = 1 to 31
  Load lblNumbers(lblNumbers.UBound + 1)
  With lblNumbers(lblNumbers.UBound)
    .Top = lblNumbers(lblNumbers.UBound - 1).Top + .Height
    .Left = 10
    .Caption = cnt
    .Visible = True
  End With
Next cnt
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Accepted Solution

by:
ameba earned 200 total points
ID: 6212637
To add labels at design time, use some add-in or create your own.
Simple sample:
Start new Add-In project (menu File, New Project, AddIn) - this will create Connect.Dsr and frmAddIn form.
Replace code in frmAddIn with this:

' frmaddIn, add one textbox (2 buttons are already on form)
Option Explicit
Public VBInstance As VBIDE.VBE
Public Connect As Connect

Private Sub CancelButton_Click()
    Connect.Hide
End Sub

Private Sub Form_Load()
    Caption = "Enter number of labels on new form"
    Text1.Text = "31"
End Sub

Private Sub OKButton_Click()
    Dim myC As VBControl, thisF As VBForm, i As Integer
    On Error Resume Next
   
    ' add new form
    Set thisF = VBInstance.VBProjects(1).VBComponents.Add(vbext_ct_VBForm).Designer
    For i = 1 To CInt(Text1.Text)
        Set myC = thisF.ContainedVBControls.Add("VB.Label")
        myC.Properties("Visible") = True
        myC.Properties("left") = 300
        myC.Properties("top") = i * 225
        myC.Properties("name") = "Label" & CStr(i)
        myC.Properties("height") = 225
        myC.Properties("width") = 2775
        myC.ControlObject.Caption = CStr(i)
    Next
End Sub
' -------------------------------------
To test addin, press F5, start new instance of VB, and click menu Add-Ins, My Addin.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6213138
ameba,
thats interesting.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6453660
Why did you punish me with a B ???

If something wasn't clear, you should have asked for clarification or for more info.

Do NOT expect any further help from me!
0
 

Author Comment

by:yeggstry
ID: 6453672
grow up ameba

I gave you a B because your answer was sufficient, but could have been better.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6454272
>grow up ameba

Some people really don't deserve ANY HELP!
0
 

Expert Comment

by:amp072397
ID: 6711598
Well, I've found my way to this question rather indirectly. However, B grades are not given if the answer was sufficient. Yeggstry, I'll gladly post the guidelines for grading. If you did not know the answer, how could you possibly know that the answer could have been better?

Changing grade to an A...

amp
Community Support Moderator
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

636 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