Results 1 to 7 of 7

Thread: How can I order dynamically created controls?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    3
    Hi,
    I am building form with dynamic insert of controls (such as Frame and Labels).
    I've tried to put Frame on the Form, another Frame on the Frame, and afterwards a Label on the last Frame by using the Controls.Add method.
    The problem is that the Label disappear to the back of the Frame.
    Using the ZOrder method wouldn't help.
    Hoe can it be done?

  2. #2
    Guest
    Labels belong to a different layer, therefor they cannot be higher than a Frame, Button, PictureBox and other graphical controls.

    As a solution, you can place the Label inside the Frame.
    Code:
    Label1.Container = Frame1
    Label1.Move 0, 0

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    3
    Thanks.
    The problem still remain.
    I'm using this code in a function, that running each time I wish to insert a new control. The MainFrame is the first control that I'm inserting and it the container of the following label controls that I want to add but when it get to the 'Else' it giving me the error:"Invalid object use".
    What can I do to solve it?

    Dim MyControl As Control
    Set MyControl = Form.Controls.Add(ControlType, ControlName, Form)
    Static y As Control
    If MyControl.Name = "MainFrame" Then
    Set y = MyControl
    Else
    Set MyControl.Container = y
    End If

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Maybe the 'Static' word?

    Just a wild guess, curse me if I am wrong.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    3
    You were half right - but don't worry, you weren't half cursed.
    The word 'static' wasn't really necessary so I dropped it and use 'Dim' instead, but it didn't solve the problem.
    When I'm using debug I can see that after inserting new control in the name of MyControl (of type such as Label) to the Form while the Frame 'MainFrame inserted before , the situation is like that:
    'MyControl.Container' is of type Variant/Object/Form1 while
    'y' is of type Control/Frame
    Maybe this will explain the problem and you'll be able to help me.

  6. #6
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I found a thread earlier in the week about dynamically creating controls. I can't find it again but kept the code and have modified it, the following code will create a label on the frame control. I am probably telling you what you already know but this seemed to work well for me.

    Code:
    Private WithEvents btnObj As Label
    
    Private Sub Command1_Click()
       Set btnObj = Controls.Add("VB.Label", "btnObj")
       Set btnObj.Container = Frame1
    
       With btnObj
          
          .Visible = True
          .Width = 2000
          .Caption = "Hello"
          .Top = 1000
          .Left = 1000
          .BackColor = vbRed
          .ForeColor = vbWhite
          .FontBold = True
          .FontSize = 14
       End With
    End Sub
    Because Frame1 is an object you do have to use the set command as you have done. I think a part of the problem is you are using FORM as the third parameter of the Add method for the controls collection of the form. I however do not specify a third parameter.

    I also use the withevents keyword to allow me access to the events of the dynamiclly created control.

    Let me know if any of this helps.


    Things I do when I am bored: DotNetable

  7. #7
    New Member
    Join Date
    Nov 2000
    Posts
    14

    label

    I encountered the label problem you mentioned, I fixed it by using a textbox instead. It works equally well. Just need to remember to set a size for the text box so that the text can fit inside it. Hope this helps.

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