Results 1 to 5 of 5

Thread: can u add code to objects created on the fly?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    22

    can u add code to objects created on the fly?

    can u add code to objects created on the fly?

    For example if i run the following code then i want to run some code if a command icon is clicked.

    Static X As Integer
    X = X + 1
    Set Commandbject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
    With CommandObject
    .Caption = "Name"
    .Top = 500
    .Left = 500
    .Height = 500
    .Width = 500
    .Visible = True
    End With

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: can u add code to objects created on the fly?

    Like this, perhaps;
    Code:
    Dim WithEvents commandobject As CommandButton
    
    Private Sub Command1_Click()
    Static X As Integer
    X = X + 1
    Set commandobject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
    With commandobject
    .Caption = "Name"
    .Top = 500
    .Left = 500
    .Height = 500
    .Width = 500
    .Visible = True
    End With
    End Sub
    
    Private Sub commandobject_Click()
    Debug.Print "click"
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    22

    Re: can u add code to objects created on the fly?

    Quote Originally Posted by Doogle
    Like this, perhaps;
    Code:
    Dim WithEvents commandobject As CommandButton
    
    Private Sub Command1_Click()
    Static X As Integer
    X = X + 1
    Set commandobject = Me.Controls.Add("VB.CommandButton", "CommandButton" & X)
    With commandobject
    .Caption = "Name"
    .Top = 500
    .Left = 500
    .Height = 500
    .Width = 500
    .Visible = True
    End With
    End Sub
    
    Private Sub commandobject_Click()
    Debug.Print "click"
    End Sub

    the problem with that is it only runs the code on the last command icon created

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: can u add code to objects created on the fly?

    One of the downsides. Try this post which provides a suitable substitute:
    http://vbforums.com/showthread.php?t=372811

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: can u add code to objects created on the fly?

    I always find it easier to do it this way. Add a Command Button to your form in the IDE, set its Index property to 0 and depending on your needs, set its Visible property to False. Then

    Code:
    Static X As Integer
    X = X + 1
    If X = 1 Then
        Command1(0).Visible = True
        Exit Sub
    End If
    
    Load Command1(X - 1)
    With Command1(X - 1)
        .Caption = "Name"
        .Top = 500
        .Left = Command1(X - 2).Left + 500 ' 500
        .Height = 500
        .Width = 500
        .Visible = True
    End With

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