Results 1 to 5 of 5

Thread: How to get a "click" in an component created at runtime ?

  1. #1

    Thread Starter
    Hyperactive Member Jlarini's Avatar
    Join Date
    Jan 2002
    Location
    São Paulo, Brazil
    Posts
    263

    Exclamation How to get a "click" in an component created at runtime ?

    Hi, guys!

    I'm in trouble with it...

    I created an array of command buttons (for instance), at runtime, and I need to know which was clicked...

    But when I click anyone I have no answer...

    What's going on ?

    Follows the code where I create the array of command Buttons:

    VB Code:
    1. Option Explicit
    2. Dim lbl(4) As Label
    3.  
    4. Private Sub lbl1_Click()
    5.     MsgBox chkbox.Name
    6. End Sub
    7.  
    8.  
    9.  
    10. Private Sub Form_Load()
    11.     Dim x As Byte
    12.  
    13.     For x = 0 To 4
    14.    
    15.     Set lbl(x) = Me.Controls.Add("VB.Label", "lbl" & Me.Controls.Count) '& "(" & x - 1 & ")")
    16.     With lbl(x)
    17.             .Left = 1000
    18.             .Top = 500 * x
    19.             .Width = 2000
    20.             .Height = 500
    21.             .Caption = lbl(x).Name
    22.             .Visible = True
    23.     End With
    24.     Next x
    25. End Sub
    26.    
    27.      
    28.      
    29.     Private Sub Form_Unload(Cancel As Integer)
    30.      
    31.         Dim i As Integer
    32.  
    33.         For i = 0 To 4
    34.             Set lbl(i) = Nothing
    35.         Next i
    36.          
    37.     End Sub

    tks!

    João Luiz
    nothing is impossible, it's sometimes very hard to do!

    If your thread is solved... Please edit it and add [Resolved] or [Solved] on it!

    If you like Marine aquarium, feel free to PM me.

    Sorry my bad English

    God bless Parksie!

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How to get a "click" in an component created at runtime ?

    You won't be able to. You can acchive that only with single control (not a control array) with the WithEvents:
    VB Code:
    1. Option Explicit
    2.     Private [B]WithEvents[/B] cmd As CommandButton
    3.  
    4. Private Sub Form_Load()
    5.     Set cmd = Me.Controls.Add("VB.CommandButton", "cmd")
    6.         With cmd
    7.             .Caption = "Click me!"
    8.             .Visible = True
    9.         End With
    10. End Sub
    11.  
    12. Private Sub cmd_Click()
    13.     MsgBox "You clicked me!"
    14. End Sub
    If you want to have a control array then add one CommandButton at design time and change it's .Index property to 0. Then try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Command1(Command1.ubound).Caption = Command1.ubound
    5.     loadCommandButtons [B]2[/B] 'will add 2 new CommandButtons...
    6. End Sub
    7.  
    8. Private Sub loadCommandButtons(n As Integer)
    9.     For n = 1 To n
    10.         Load Command1(Command1.ubound + 1)
    11.             With Command1(Command1.ubound)
    12.                 .Move Command1(Command1.ubound - 1).Left, Command1(Command1.ubound - 1).Top + Command1(Command1.ubound - 1).Height + 100, Command1(Command1.ubound - 1).Width, Command1(Command1.ubound - 1).Height
    13.                 .Visible = True
    14.                 .Caption = Command1.ubound
    15.             End With
    16.     Next n
    17. End Sub
    18.  
    19. Private Sub Command1_Click(Index As Integer)
    20.     MsgBox Index
    21. End Sub

  3. #3

    Thread Starter
    Hyperactive Member Jlarini's Avatar
    Join Date
    Jan 2002
    Location
    São Paulo, Brazil
    Posts
    263

    Re: How to get a "click" in an component created at runtime ?

    Gavio,

    Thanks your fast answer...

    I tried the both ways you said...

    My problem is:

    If I use "WithEvents" I can't:
    - create an array...
    - change the component's name...

    If I use "Set" I must to create a lot of controls, cos I have a "crazy" system, where I need to make severous arrays of controls...

    Such this:

    Label (0)
    Label1 (0) check1(0)
    Label1 (1) check1(1)
    Label1 (2) check1(2)

    Label (1)
    Label2 (0) check2(0)
    Label2 (1) check2(1)
    Label2 (2) check2(2)

    And I have no Idea how many label(X), Labelx(x) and checkx(x) I'll need at each time...

    []s

    João Luiz
    nothing is impossible, it's sometimes very hard to do!

    If your thread is solved... Please edit it and add [Resolved] or [Solved] on it!

    If you like Marine aquarium, feel free to PM me.

    Sorry my bad English

    God bless Parksie!

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How to get a "click" in an component created at runtime ?

    Quote Originally Posted by Jlarini
    ... If I use "WithEvents" I can't:

    - create an array...
    Emm...
    Quote Originally Posted by me
    You won't be able to. You can acchive that only with single control (not a control array) with the WithEvents...

  5. #5
    Hyperactive Member
    Join Date
    May 2006
    Posts
    365

    Re: How to get a "click" in an component created at runtime ?

    Jlarini,

    I decided to cheat when I needed an array of command buttons by creating a command button at design time that was visible=false and using that command button to make copies of.
    Two major benefits for me to using this method were:
    firstly I had multiple copies of the command buttons click event and second It was of great benefit to me to have the first usable button in my array numbered as 1.

    Steve

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