Solved

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

Posted on 2001-06-20
9
152 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
  • 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
 
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
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.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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 …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now