Solved

Passing Controls to Active X Components

Posted on 2001-06-21
10
238 Views
Last Modified: 2013-11-25
Is it possible to pass a control, say List Box, to an Active X to populate?

e.g The Active X populates List1 with numbers 1 - 5, I want to call this several times in several apps but pass different listbox references to List1.

Cheers,
R
0
Comment
Question by:rallsaldo
  • 4
  • 4
  • 2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6214479
Yes, it is possible...
Have you some specific problem with this?
You might note that the parameter type should be Control and not Listbox...

Public Sub FillListBox ( list as Control )
  dim l as ListBox
  set l = list

  l.AddItem "1"
  l.AddItem "2"
  ...

End Sub

CHeers
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6214609
Cheers a,

I have tried the control approach....

What exactly I am trying to do is have a private reference to mlstListBox in my class. I have a Let statement which allows me to set the value of mlstListBox to whatever Listbox name I choose, then a public sub which populates this mlstListBox.

Thus enabling me to have a dll which populates a listbox with a set of items regardless of what listbox I pass.

Currently it is falling over at the Let statement with the comment "Private object modules cannot be used in public object models as parameters or return types for public procedures, as public data members, or as fields of publuc user defined types"

Cheers,
R
0
 
LVL 15

Expert Comment

by:ameba
ID: 6214756
There are two ways to solve this.

' 1. Pass Listbox 'As Object' - this is Late bound call (slow)
' Class1 in ActiveX Dll
Option Explicit

Public Sub Populate(List As Object)
    List.Clear
    List.AddItem "Hello"
End Sub

' usage:
Private Sub Form_Click()
    Dim x As Project2.Class1
    Set x = New Class1
    x.Populate List1
End Sub



' 2. Early bound solution - add variable in Class1 declarations
' Class1 in ActiveX Dll
Option Explicit
Public List As ListBox

Public Sub Populate()
    If Not List Is Nothing Then
        List.Clear
        List.AddItem "Hello"
    End If
End Sub

' usage:
Private Sub Form_Click()
    Dim x As Project2.Class1
    Set x = New Class1
    Set x.List = List1
    x.Populate
End Sub
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6214759
First of all, to assign a Listbox, you need to use the SET statement...

Maybe you post your relevant code, so we can correct it?

---------Class code-------
private mlstListBox as ListBox

Public Property Set WorkListBox ( nv as Control )
  set mlstListBox = nv
End Property

Public Sub PopulateListBox()
  with mlstListBox
     .AddItem "1"
     .AddItem "2"
     ...
  end with
End Sub

--- Other code ----

with YourClass
  Set .WorkListBox = listbox1
  .Populate
  Set .WorkListBox = listbox2
  .Populate
end with



Cheers
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6214907
Cheers folks... I've pretty much tried this anyhow.

This is the code:

---Class Code---
Private mlstListBox As ListBox

Public Property Set MyListBox(vListBox As Control)
   
    Set mlstListBox = vListBox
       
End Property

This is were it is failing. If you paste it into a new Class you'll see what I mean when trying to compile.

This is probably blindingly obvious but got me stumped!!

Cheers,
R
0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 6214923
I tested both solutions I posted and they worked. I suggest 2nd solution.

' usage (code in a form with a listbox):
Private Sub Form_Click()
   Dim x As Project2.Class1
   Set x = New Class1
   Set x.List = List1
   x.Populate
End Sub
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6215242
Hi Ameba,

Yep this works fine ~ thanks ~ I've always declared variables as private in a class and then used the property let, property get to expose them to users. I take it this is different with controls?

Cheers,
R
0
 
LVL 15

Expert Comment

by:ameba
ID: 6215293
>I take it this is different with controls?

Well, if you want early binding, and if code is in a different project, yes, that is the syntax that is supported by VB.

Public X As ListBox
or:
Public WithEvents X As ListBox
0
 
LVL 15

Expert Comment

by:ameba
ID: 6217727
B?  What was wrong with my answer?  You got perfect expertize on how to do what you want with code samples (both code samples required creating program group)

Do not expect any further help from me!
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6218701
Ameba,

It was my first question asked and I was simply following the guidleines set out on the response page...

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

If you don't think that I awarded fairly then I apologise, I am grateful for your help, perhaps I should award A's to everyone... then everyones happy.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Analytics 5 18
TT Auto Dashboard 13 92
Please explain "Multi-Tenant Services" 5 86
How to read File Date Created using VB6 8 39
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question