Vba 在userform初始化之后更新userform列表框

Vba 在userform初始化之后更新userform列表框,vba,excel,userform,Vba,Excel,Userform,是否有任何方法可以在UserForm\u Initializesub之外更新UserForm上的ListBox 为什么? 我正在构建一个21点游戏,并使用列表框告诉用户他们有/经销商有什么牌。我希望使用一个简单的子(ShowCards)将项目添加到列表框中,但遇到了以下问题: Play按钮调用位于普通模块中的PlayBlackjack子模块 Option Explicit Dim cards As New Collection Sub ShowGame() UFDisplay.Sh

是否有任何方法可以在
UserForm\u Initialize
sub之外更新
UserForm上的
ListBox

为什么? 我正在构建一个21点游戏,并使用列表框告诉用户他们有/经销商有什么牌。我希望使用一个简单的子(
ShowCards
)将项目添加到列表框中,但遇到了以下问题:

Play按钮调用位于普通模块中的PlayBlackjack子模块

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
选项显式
暗卡作为新的收藏
子游戏()
UFDisplay,Show
端接头
亚21点游戏()
'用5个洗牌组填充卡片集合
准备卡片
作为整数的Dim i
Dim userHand作为新集合
Dim dealerHand作为新系列
'发牌(从卡集合中删除发牌)
对于i=1到2
用户卡
DealCard卡,dealerHand
接下来我

ShowCards userHand,UFDisplay.UserHandList您也可以使用一个类作为hand,并将类listbox设置为表单listbox,例如,类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
编辑:建议

为你的卡片使用数字和套装名称,然后你可以执行以下操作,比如启用拆分按钮,顺便说一句,你将使用一个手数组,然后使用arrHands(x)


看看如何充分利用类的潜力,也看看事件,对某些事情做出反应。

啊,我明白了。不能将listBox参数声明为listBox。后者保留给Activex控件,而不是VBA控件。将
展示卡的签名更改为:

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

Private Sub ShowCards(作为集合的手,作为控件的列表)'它将告诉我。userhandlist@CallumDA很高兴为您提供帮助:)谢谢您,我也开始学习了
手工
课程,但我没有充分使用它,觉得这是必要的-这是一个有趣的想法。我要好好玩玩它,看看它是否能让事情变得更整洁谢谢,这是一个很好的建议-我测试过了,效果非常好。我想我会把我的最后一段代码发布在代码审查上,看看我离一个像样的东西有多近。总的来说,非常感谢你的帮助。现在我接受了A.S.H的答案,因为我认为这对其他人来说是最有用的。不用担心,但正确的解决方案是
公共子名称(x As MSForms.ListBox)
:)祝你有个愉快的一天。
Private Sub ShowCards(hand As Collection, list As Control) '<~~ or MSForms.ListBox, or simply as Object...