Updating the list of user forms after initializing the user form

Is there a way to upgrade ListBoxto an UserFormoutside Userform_Initializesub?

Why? I create a blackjack game and use lists to tell the user which cards they have / the dealer has. I was hoping to use simple sub ( ShowCards) to add items to the list, but I ran into problems:

enter image description here

The Play button calls up the PlayBlackjack element , which is located in the normal module

Option Explicit

Dim cards As New Collection

Sub ShowGame()
    UFDisplay.Show
End Sub

Sub PlayBlackjack()
    'fill the cards collection with 5 shuffled decks
    PrepareCards

    Dim i As Integer
    Dim userHand As New Collection
    Dim dealerHand As New Collection

    'deal cards (removing the dealt cards from the cards collection)
    For i = 1 To 2
        DealCard cards, userHand
        DealCard cards, dealerHand
    Next i

    ShowCards userHand, UFDisplay.UserHandList <-- ERROR HERE (Type mismatch)

    'more code to follow
End Sub

Private Sub ShowCards(hand As Collection, list As ListBox)
    Dim i As Integer

    For i = 1 To hand.Count
        list.AddItem hand(i).CardName
    Next i
End Sub

Let me know if you think you need more code. handis a collection of map classes where it .CardNamereturns something like3 of Hearts

, , , , , - . Userform.Repaint .

, , userHand dealerHand , Useform_Initialize, ? , , , .

. , -, ( )

# 1 ShowGame , PlayBlackjack Userform ( )

+4
2

, . listBox ListBox. Activex, VBA. ShowCards sub :

Private Sub ShowCards(hand As Collection, list As Control) '<~~ or MSForms.ListBox, or simply as Object...
+2

, clsHand

Public colHand As collection
Public lstToUpdate As MSForms.ListBox

Private Sub Class_Initialize()
    Set colHand = New collection
End Sub

Friend Function AddCard(card As clsCard)
    colHand.Add card, CStr(colHand.Count)
    If Not lstToUpdate Is Nothing Then
        lstToUpdate.AddItem card.strCardName
    End If
End Function

Private clsPlayerHand As clsHand

Private Sub UserForm_Initialize()
    Set clsPlayerHand = New clsHand
    Set clsPlayerHand.lstToUpdate = Me.ListBox1
End Sub

Private Sub CommandButton1_Click()
    Dim clsC As New clsCard
    clsC.strCardName = "one"
    clsPlayerHand.AddCard clsC
End Sub

EDIT: ,

, , , , , arrHands (x)...

Public colHand As collection
Public lstToUpdate As MSForms.ListBox
Public cmdSplitButton As MSForms.CommandButton

Private Sub Class_Initialize()
    Set colHand = New collection
End Sub

Friend Function AddCard(card As clsCard)
    colHand.Add card, CStr(colHand.Count)
    If Not lstToUpdate Is Nothing Then
        lstToUpdate.AddItem card.CardName
    End If
    If Not cmdSplitButton Is Nothing Then
        If colHand.Count = 2 Then _
        cmdSplitButton.Enabled = colHand(1).NumericPart = colHand(2).NumericPart
    End If
End Function

, .

+3

Source: https://habr.com/ru/post/1663537/


All Articles