Results 1 to 8 of 8

Thread: [RESOLVED] Make new forms and buttons and stuff in the program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Resolved [RESOLVED] Make new forms and buttons and stuff in the program

    Is there a way to make new forms and buttons when the programs running?

  2. #2
    Hyperactive Member Bearnerd's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    290

    Re: Make new forms and buttons and stuff in the program

    Maybe this link will help you. The problem is more or less the same like yours. Try to read this thread

    http://www.vbforums.com/showthread.p...create+control

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Make new forms and buttons and stuff in the program

    @Bernard: Well that link for a different programming language, so i'm not sure that's going to help.

    @Ahpro: Yes it is possible, what are you trying to achieve?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Re: Make new forms and buttons and stuff in the program

    there's actually another question coming with this aswell. say i want it to create a new label when i press a button, how would i 1. create the label and 2. make it have the same name as th elast label just the number on the end is the last one +1 so i don't end up with 2 or more having the same name.

  5. #5
    Hyperactive Member Bearnerd's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    290

    Re: Make new forms and buttons and stuff in the program

    I found a thread on how to create a new command button at runtime. Just go to the link below

    http://www.vbforums.com/showthread.php?t=342054

    Maybe this is the answer to your question. If it is so, please rate

    VB Code:
    1. Option Explicit
    2. Dim WithEvents testLabel As Label
    3.  
    4. Private Sub Command1_Click()
    5.     Set testLabel = Form1.Controls.Add("VB.label", "newLabel")
    6.      
    7.     'Set the properties of the new label
    8.     With testLabel
    9.         .Left = 1000
    10.         .Top = 1000
    11.         .Width = 2000
    12.         .Height = 500
    13.         .Caption = "Hello this is a new label!"
    14.         .Visible = True
    15.      End With
    16. End Sub
    17.  
    18. Private Sub Form_Unload(Cancel As Integer)
    19.     Set testLabel = Nothing 'clear the memory
    20. End Sub

  6. #6
    Addicted Member Redangel's Avatar
    Join Date
    Oct 2005
    Location
    England
    Posts
    214

    Re: Make new forms and buttons and stuff in the program

    there's actually another question coming with this aswell. say i want it to create a new label when i press a button, how would i 1. create the label and 2. make it have the same name as th elast label just the number on the end is the last one +1 so i don't end up with 2 or more having the same name.
    VB Code:
    1. Option Explicit
    2. Dim WithEvents lbl As Label
    3. Dim i As Integer
    4.  
    5. Private Sub Command1_Click()
    6.     i = i + 1
    7.     Set lbl = Form1.Controls.Add("VB.Label", "lblTest" & i)
    8.     With lbl
    9.         .Caption = "lblTest" & i
    10.         .Visible = True
    11.         .Top = i * 500
    12.         'Set the Left Position Etc.
    13.     End With
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.     i = -1
    18. End Sub
    19.  
    20. Private Sub Form_Unload(Cancel As Integer)
    21.     Set lbl = Nothing
    22. End Sub

    if i helped then please rate my post

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    221

    Re: Make new forms and buttons and stuff in the program

    you're both stars, thanks a lot. i'll rate both of you well.

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Make new forms and buttons and stuff in the program

    In your situation (and most others too) it's best to have a control already on the form and just load another member of the array.

    Place a label on the form (name it lblText) and set Index = 0, then:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Load lblText(lblText.UBound + 1)
    3.     With lblText(lblText.UBound)
    4.         .Caption = "Hello"
    5.         .Visible = True
    6.     End With
    7. End Sub
    8.  
    9. ' Any events for this new label can be dealt with in the usual way
    10. Private Sub lblText_Click(Index As Integer)
    11.     MsgBox "Click on Label: " & Index
    12. End Sub
    13.  
    14. ' Make sure you unload all but the last member of the array upon form unload
    15. Private Sub Form_Unload(Cancel As Integer)
    16.     Dim N As Integer
    17.     If lblText.UBound > 0 Then
    18.         For N = lblText.UBound To lblText.LBound + 1
    19.             Unload lblText(N)
    20.         Next N
    21.     End If
    22. End Sub

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