Solved

Passing Controls to Active X Components

Posted on 2001-06-21
10
236 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now