Results 1 to 3 of 3

Thread: controls.add WITH arrays?

  1. #1

    Thread Starter
    Lively Member rotcrules's Avatar
    Join Date
    Mar 2004
    Location
    SD
    Posts
    113

    controls.add WITH arrays?

    What i am trying to do is make a mine sweeper type game. and i need to know when the commandbutton is clicked

    my code is somthing like this:

    Code:
    private WithEvents CB as CommandButton
    Private Sub Form_Load()
    ReDim CB(63)
      For i = 0 to 63
        CB(i) = (Controls.Add "VB.CommandButton","Command1")
        CB(i).Visible = true
        CB(i).Move 'exc...
      next
    end sub
    I know I cannot use the "WithEvents" and an array but is there anything else i can do?

    maybe somthing like this?:

    Dim (CB & i)
    if CB & i = true

    do you see what i mean make many vars. and at the end make a number?

    I hope i explained well

    THANKS
    NATHAN
    [Vbcode]If YourOpinion = WindowsIsCrap Then
    Kill Wndows
    Open Linux
    ElseIf YourOpinion = WindowsIsGreat Then
    Unload Me
    Else
    Get MSNTV
    End If[/vbcode]

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    What wrong with making a control array during design-time and Load() new buttons?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You can't create a control array at runtime but you can 'simulate' one using a a class module and a collection class. Basically, the Class module is used to handle everything you need for a Command button. It receives the Button_Click events from the control and passes them on to its parent, the Collection class. The Collection class in turn raises the Event to the Form.

    Pretty simple once you know the ins and outs. Probably overkil for this project but you can add as many events, properties, methods as you want.


    VB Code:
    1. 'Class module to handle CommandButton Events
    2. Private WithEvents mobjCB As CommandButton
    3. Private mlngIndex As Long
    4. Private mobjParent As MyButtonsCollection
    5.  
    6. Public Property Set MyParentCollection(Value As MyButtonsCollection)
    7.     Set mobjParent = Value
    8. End Property
    9.  
    10. Public Property Get Index() As Long
    11.     Index = mlngIndex
    12. End Property
    13. Public Property Let Index(ByVal Value As Long)
    14.     mlngIndex = Value
    15. End Property
    16.  
    17. Public Property Get Button() As VB.CommandButton
    18.      Set Button = mobjCB
    19. End Property
    20.  
    21. Public Property Set Button(ButtonControl As VB.CommandButton)
    22.      Set mobjCB = ButtonControl
    23. End Property
    24.  
    25. Private Sub Class_Terminate()
    26.     Set mobjParent = Nothing
    27.     Set mobjCB = Nothing
    28. End Sub
    29.  
    30. Private Sub mobjCB_Click()
    31.     mobjParent.RaiseButtonEvent Me
    32. End Sub
    33.  
    34. 'Collection class module to handle multiple buttons and to raise events to the Form.
    35. 'Omitted the typical Collection properties/methods like Count, Item etc..
    36.  
    37. Private mCol As Collection
    38.  
    39. Public Event ButtonClicked(Sender As MyButton)
    40.  
    41. Public Function Add(Button As VB.CommandButton, Index As Long, Optional sKey As String) As MyButton
    42.     'create a new object
    43.     Dim objNewMember As MyButton
    44.     Set objNewMember = New MyButton
    45.  
    46.  
    47.     Set objNewMember.Button = Button
    48.     objNewMember.Index = Index
    49.    
    50.     If Len(sKey) = 0 Then
    51.         mCol.Add objNewMember
    52.     Else
    53.         mCol.Add objNewMember, sKey
    54.     End If
    55.     Set objNewMember.MyParentCollection = Me
    56.    
    57.     'return the object created
    58.     Set Add = objNewMember
    59.     Set objNewMember = Nothing
    60.  
    61. End Function
    62.  
    63. Private Sub Class_Initialize()
    64.     Set mCol = New Collection
    65. End Sub
    66.  
    67. Public Sub RaiseButtonEvent(ButtonObject As MyButton)
    68.     RaiseEvent ButtonClicked(ButtonObject)
    69. End Sub
    70.  
    71. 'Form Module
    72. 'Omitted the clean up code to Remove the Controls created at runtime, delete the collection etc...
    73. Private WithEvents mcolButtons As MyButtonsCollection
    74.  
    75. Private Sub Form_Load()
    76.  
    77.     Dim objButton As MyButton
    78.     Dim lngIdx As Long
    79.    
    80.     Set mcolButtons = New MyButtonsCollection
    81.    
    82.     For lngIdx = 0 To 63
    83.         Set objButton = mcolButtons.Add(Controls.Add("VB.CommandButton", "Command" & lngIdx), lngIdx, "Command" & lngIdx)
    84.        
    85.         With objButton.Button
    86.             .Caption = "Command" & lngIdx
    87.             .Visible = True
    88.             .Move 0, 315 * lngIdx, 2000, 315
    89.         End With
    90.     Next
    91.  
    92. End Sub
    93.  
    94. Private Sub mcolButtons_ButtonClicked(Sender As MyButton)
    95.     Debug.Print Sender.Button.Caption, Sender.Index
    96. 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