Mathieu Guindon
Mathieu Guindon

Reputation: 71167

Populate VB6 ListBox with single instruction

In VB6 I can't seem to be able to do that:

myListBox.List = listContentStringArrray

VB is complaining with a compile error saying "Argument not optional", obviously because .List(index) requires, well, an index.

The following code works in VBA:

Private Sub CommandButton1_Click()
    Dim result() As String
    ReDim result(0)
    result(0) = "hello"
    ReDim Preserve result(1)
    result(1) = "world"

    ListBox1.List = result

End Sub

Looking at the object browser I found that the ListBox in VBA is really MSForms.ListBox and in VB6 it's VB.ListBox.

The VBA ListBox's List property is declared as Property List([pvargIndex], [pvargColumn]) and in VB6 it's Property List(Integer) As String, which is where my problem comes from.

Is there a way to "swap" VB.ListBox for MSForms.ListBox in an existing code base without breaking anything, or MSForms was designed for use only in VBA as the "VB" library isn't included in VBA? If I reference FM20.dll in my VB6 project, I can see the MSForms.ListBox in the object browser, but not in the toolbox.

Upvotes: 2

Views: 2591

Answers (1)

Ilya Kurnosov
Ilya Kurnosov

Reputation: 3220

It is possible to use Forms 2.0 controls in VB6 application. For instance, it is one of ways to get controls that support Unicode (see How To Read and Display UNICODE String on Visual Basic Form KB article).

If I reference FM20.dll in my VB6 project, I can see the MSForms.ListBox in the object browser, but not in the toolbox.

To add the controls to the toolbox you have to add Microsoft Forms 2.0 Object Library via Project | Components menu item (Ctrl+T): enter image description here

But, frankly, if all you need is an ability to 'populate VB6 ListBox with single instruction' it is a huge overkill. Just code a routine that does it for regular VB6 ListBox and you're done.

Also keep in mind that Forms 2.0 controls are not redistributable. From INFO: Usage and Redistribution of FM20.DLL KB:

The Fm20.dll is NOT redistributable. You must have an application such as Microsoft Office 97 on the target system that installs Fm20.dll as part of its setup... In any case, you may not distribute the Fm20.dll as part of your setup, even if you purchase the Microsoft Office Developer Edition product.

As an alternative to having your end users install Microsoft Office, you can have them freely download and install the Microsoft ActiveX Control Pad, which also installs the Fm20.dll.

Upvotes: 3

Related Questions