Results 1 to 12 of 12

Thread: How to add additional object to form through code

  1. #1

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    How to add additional object to form through code

    Hi.

    I would like to know how to add additional objects like eg a textbox to a form through code for an event.

    Thanks.
    The God of Death Lives Again

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: How to add additional object to form through code

    Code:
    Private Sub Form_Load()
    Dim futureLabel As Label
            Set futureLabel = Me.Controls.Add("VB.Label", "futureLabel")
            futureLabel.Height = 300
            futureLabel.Width = 550
            futureLabel.Top = 250
            futureLabel.Left = 250
            futureLabel.Caption = "testing"
            futureLabel.Visible = True
    End Sub

  3. #3

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    Re: How to add additional object to form through code

    Hi Zack_vb6.

    How would I be able to add these controls to say a picbox so that I can have them grouped and be able to scroll through them.

    I dont have vb on this pc. I will have to try it out at home and let you know.

    Thanks.
    The God of Death Lives Again

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to add additional object to form through code

    set futurelabel.container = picturebox1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    Re: How to add additional object to form through code

    Thanks Guys. Tried it at home and it works great.
    Another question. How would you create a control array of a control through code?
    The God of Death Lives Again

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to add additional object to form through code

    no can do, you can create all the controls you want at runtime, but control arrays have to be created at design time
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    Re: How to add additional object to form through code

    Ouch.

    Then say I create Label1 to Label10 at runtime.
    How can I reference the properties of the controls to simulate an array?

    would it be something like: me.controls("Label" & i).caption="hello"
    where i is a variable string?
    The God of Death Lives Again

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to add additional object to form through code

    You would do something like
    Code:
    Private Sub lblLabel_Click(Index As Integer)
    Select Case Index
        Case 0
          lblLabel(0).Caption = "Hello"
        Case 1
          lblLabel(1).Caption = "GoodBye"
        'etc
    End Select
    End Sub

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to add additional object to form through code

    Actually, what you could do is create an array, then as you create the labels, add them to the array... then you should be able to access them through a pseudo control array (it would be an array of controls instead (or Label)..)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    Re: How to add additional object to form through code

    Thanks Hack.


    But how would I reference the different label names?
    westconn1 said you cannot create a control array at runtime.

    techgnome, how would I go about doing what you suggest?

    dim arrLabel(0 to 10) as label
    dim i as integer

    for i=0 to 10
    Set arrLabel(i) = Me.Controls.Add("VB.Label", "futureLabel" & i)
    next

    for i=0 to 10
    arrLabel(i).caption="Hello" & i
    next
    The God of Death Lives Again

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to add additional object to form through code

    Why not put one label at design time on the form with Index = 0, then Load the rest of the labels at run time. That way you can get the events from the control also... while if you load a label/control using Me.Controls.Add() you won't get the events from the control.

  12. #12

    Thread Starter
    Lively Member Brenton's Avatar
    Join Date
    Jan 2002
    Location
    Look Up!
    Posts
    125

    Re: How to add additional object to form through code

    Basically this is what I want to use this for:

    I got a db that is full of items. Now depending on the number of items in the db, for each item, I want to represent it with a label or shape. Now since at design time I have no idea of how many items I have in the db, I dont know how many shape controls to add to the form.
    The God of Death Lives Again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width