首页 > 解决方案 > 使用类模块的列表框

问题描述

在 visual basic 6.0 中,我正在处理数组和列表框。我希望当我单击命令按钮时,所有字符串值都将显示在列表框中,因此我想使用类中的对象并在表单中调用它。我想知道如何将列表框的字符串值从类模块调用到表单。

我已经尝试过字符串数组,但仅用于消息框。我不知道如何使用列表框。我可以展示我所做的。我使用 class1 创建了一个方法 friendslist()。正如那里看到的,我使用了消息框,我想用文本替换它。然后在 command1_click() 中调用这些文本作为列表框的值

Dim friends(5) As String
friends(0) = "Anna"
friends(1) = "Mona"
friends(2) = "Marie"
friends(3) = "Kent"
friends(4) = "Jona"
friends(5) = "Fatima"

For a = 0 To 5
MsgBox "Your friends are: " & friends(a)
Next
End Sub

Private Sub Command1_Click()
Dim myfriends As New Class1
Call myfriends.friendslist

End Sub

这是我的预期输出

表格1

标签: arraysmodulelistboxvb6

解决方案


您可以将 ListBox 作为参数传递给 friendslist() 方法。

' insert this code into Class1
Public Sub FriendsList(oList As ListBox)
    Dim a As Long
    Dim friends(5) As String
    friends(0) = "Anna"
    friends(1) = "Mona"
    friends(2) = "Marie"
    friends(3) = "Kent"
    friends(4) = "Jona"
    friends(5) = "Fatima"

    oList.Clear
    For a = LBound(friends) To UBound(friends)
        oList.AddItem friends(a)
    Next a
End Sub

' insert this code into form
Private Sub Command1_Click()
    Dim oFriends As Class1
    Set oFriends = New Class1
    oFriends.FriendsList List1    ' instead of List1, type the actual name of ListBox control
    Set oFriends = Nothing
End Sub

推荐阅读