Results 1 to 10 of 10

Thread: Events during runtime

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    I need to add events to a new object during runtime. This is what I have so far:
    Code:
    Dim Form_To_Create As Form
    Form_to_Create.Caption = "Test"
    Form_To_Create.Show
    How would I add code to the Form_Click Event?
    Also, how would you put an image over another image?
    I tried a picturebox over another but you saw the square of the object over the form, but I would like to be able to see just the image in front. Thanks.
    retired member. Thanks for everything

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That won't work, to actually create the form you need to use New keyword and a class, in this case form1, which you would have to do in designtime
    Code:
    Dim Form_To_Create As Form
    Set Form_To_Create = New Form1 '<---
    Form_To_Create.Caption = "Test"
    Form_To_Create.Show
    On the other hand this form will be created and can stil be changed during runtime
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    But how can I add code to Form1_Click of the new Form after I create it?
    retired member. Thanks for everything

  4. #4
    Guest
    Use the WithEvents statement.

  5. #5

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    How exactly?
    retired member. Thanks for everything

  6. #6
    Guest
    This will add an event for a button created at runtime
    Code:
    Private WithEvents cmd As CommandButton
    
    Private Sub Form_Load()
        Set chkBox = Me.Controls.Add("VB.CommandButton", "Btn")
        Me!Btn.Move 0, 0
        Me!Btn.Caption = "Button1"
        Me!Btn.Visible = True
    End Sub
    
    Private Sub cmd_Click()
        MsgBox "You pressed the button"
    End Sub

  7. #7

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    Please apply this...

    How would you have a button, that when clicked, created a button that, when clicked, popped up a MsgBox that said "hello, you have clicked the second Command Button that was created during runtime by another Command button."?
    retired member. Thanks for everything

  8. #8
    Guest
    Add the following to a Form with 1 CommandButton.
    Code:
    Private WithEvents cmd As CommandButton
    
    Private Sub Command1_Click()
        Set cmd = Me.Controls.Add("VB.CommandButton", "Btn")
        Me!Btn.Move 0, 0
        Me!Btn.Caption = "NewButton"
        Me!Btn.Visible = True
    End Sub
    
    Private Sub cmd_Click()
        MsgBox "hello, you have clicked the second Command Button that was created during runtime by another Command button"
    End Sub

  9. #9

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    Talking last question!

    When I do the above it only works to create one. What would I do to create another after another button that say "hello, you have clicked the second Command Button that was created during runtime by another Command button" when you click the original Button? Now, it get an error saying there is already a Btn on the form.
    retired member. Thanks for everything

  10. #10

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Also, without arrays. In other words, have the function creat a button and add the name to a list box. You can click the original button again and again, each time adding a Btn1, Btn2 and so forth(remember, no arrays) and when you click the new buttons, they all do the same things (i.e. MsgBox "you clicked me")Anyone?
    retired member. Thanks for everything

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