GeorgeVT
asked on
Create a public collection in a userControl
I am trying to create an array of a object (from a class) or a user defuned type in a UserControl. I need to be able to access the properties of the class inside the UserControl.
I have created a collection that works fine inside the UserControl, But I can't Get to it from outside. VB won't let met declare the Collect as Public, or make it a property of the control.
Was there something I need to do or any work around?
I have created a collection that works fine inside the UserControl, But I can't Get to it from outside. VB won't let met declare the Collect as Public, or make it a property of the control.
Was there something I need to do or any work around?
A little more infor please?
The simplest solution is to expose properties of your class via UserControl; e.g. if class is SubItem, and property is BackColor, your users will not use:
Dim objX As SubItem
MsgBox objX.BackColor
but this:
MsgBox myControl.SubItemBackColor ' no extra objects
Dim objX As SubItem
MsgBox objX.BackColor
but this:
MsgBox myControl.SubItemBackColor
Or with 2D array, instead of
Dim objX As Cell
objX(6, 7).BackColor = vbRed
use:
myControl.Row = 6
myControl.Col = 7
myControl.CellBackColor = vbRed
Dim objX As Cell
objX(6, 7).BackColor = vbRed
use:
myControl.Row = 6
myControl.Col = 7
myControl.CellBackColor = vbRed
Welll.. some extra info might be helpfull... but I'l give it a try.
I guess you have an internal Collection of objects in you UserControl. But, did you declare the classmodule, which is being held in that collection as PublicNotCreateable or Public (Instancing Property of classmodule) ?
Second, as ameba mentioned, you should implement a property or a method on the usercontrol in order to get to the internal collection object from the containing form. Something like:
In UserControl:
Public Property Get MyCollection() as Collection
Set MyCollection = m_InternalCollection
End Property
On the Form you can then:
Dim oCollectionInForm as Collection
Set oCollectionInForm = MyControl.MyCollection
If you are doing all of this, it should work. Some extra info will definitely be needed to figure out what else is going wrong otherwise....
Hope this can help
Patrick.
I guess you have an internal Collection of objects in you UserControl. But, did you declare the classmodule, which is being held in that collection as PublicNotCreateable or Public (Instancing Property of classmodule) ?
Second, as ameba mentioned, you should implement a property or a method on the usercontrol in order to get to the internal collection object from the containing form. Something like:
In UserControl:
Public Property Get MyCollection() as Collection
Set MyCollection = m_InternalCollection
End Property
On the Form you can then:
Dim oCollectionInForm as Collection
Set oCollectionInForm = MyControl.MyCollection
If you are doing all of this, it should work. Some extra info will definitely be needed to figure out what else is going wrong otherwise....
Hope this can help
Patrick.
GeorgeVT>
You need to create your own Class as a wrapper for the Collection Object.
If you have VB5+ you canuse the Class Wizard to do all the hardwork, otherwise
you need to create a class with all the properties of the Collection
ie
Add, Remove, Item, Count
Then make that class MultiUse, or PublicNonCreateAble(Most Likely)
IF you use the class wizard , you even get the For Each .. NExt functionality
Hope this helps
Gordon
You need to create your own Class as a wrapper for the Collection Object.
If you have VB5+ you canuse the Class Wizard to do all the hardwork, otherwise
you need to create a class with all the properties of the Collection
ie
Add, Remove, Item, Count
Then make that class MultiUse, or PublicNonCreateAble(Most Likely)
IF you use the class wizard , you even get the For Each .. NExt functionality
Hope this helps
Gordon
ASKER
I already did that, Gordonp. You did not understand my problem.
Here is the more information:
I am doing a UserControl for a Doctor's Office. They use Carousel kind of shelf to keep patient's records. Each carousel has a number of shelves, and each shelf has a number of slots, and in each slot hold a patient's records.
So I have a class for slots with a couple of properties, SlotID, ShelfNumber it is in, and PatientName.
I developed a collection of slots with the patient Name as the key. Now I have a user control for the carousel which also have a MSComm to control the turning, but that part works fime. i also have a database that the control save/retrieve infor from, it also works fine.
So Noe I would like to do is, in My VB form, drop the UserControl to the form, and be able to use something like;
1. When I get a new Patient, I need to assign a empty slot to the patient, to do something like:
MyCarousel.Slots(1234).Pat ient = "Bob Jones"
2. When a patient comes in, I need to retieve the slot number so I can turn it to the right shelf for the clerk to get to the record. something like
ShelfNumber = MyControl.Slots("Sandy Wonner").ShelfNumber
Thanks for your help
Here is the more information:
I am doing a UserControl for a Doctor's Office. They use Carousel kind of shelf to keep patient's records. Each carousel has a number of shelves, and each shelf has a number of slots, and in each slot hold a patient's records.
So I have a class for slots with a couple of properties, SlotID, ShelfNumber it is in, and PatientName.
I developed a collection of slots with the patient Name as the key. Now I have a user control for the carousel which also have a MSComm to control the turning, but that part works fime. i also have a database that the control save/retrieve infor from, it also works fine.
So Noe I would like to do is, in My VB form, drop the UserControl to the form, and be able to use something like;
1. When I get a new Patient, I need to assign a empty slot to the patient, to do something like:
MyCarousel.Slots(1234).Pat
2. When a patient comes in, I need to retieve the slot number so I can turn it to the right shelf for the clerk to get to the record. something like
ShelfNumber = MyControl.Slots("Sandy Wonner").ShelfNumber
Thanks for your help
This is my 2 cents, George.
Fisrt add a new method to your collection, called changeName which would change the patientname of the slot (if you need help on that, let me know, I will try that)
Add a new method to your userControl, call it PatientName , with one requred parameter of SlotID(assume that is the key to your collection), and Optional Parameter of PatientName (declare it as Variant)
In the new method:
Do something like:
Dim myslot as Slot
If IsMissing(PatientNAme) then
'Get Name
Set MySlot = YourCollection.Item(SlotID )
PatientName = MySlot.patientNAme
Else
call yourCollection.ChangeName( SlotID, NewNAme)
End If
Fisrt add a new method to your collection, called changeName which would change the patientname of the slot (if you need help on that, let me know, I will try that)
Add a new method to your userControl, call it PatientName , with one requred parameter of SlotID(assume that is the key to your collection), and Optional Parameter of PatientName (declare it as Variant)
In the new method:
Do something like:
Dim myslot as Slot
If IsMissing(PatientNAme) then
'Get Name
Set MySlot = YourCollection.Item(SlotID
PatientName = MySlot.patientNAme
Else
call yourCollection.ChangeName(
End If
If you have your collction class set as PublicNonCreatable you should be able to expose it.
in your USercontrol try
Private m_Slots as clsSlots
Public Property Get Slots() as clsSlots
Set Slots = m_Slots
End Property
Gordon
If this wont work then what error are you getting when you try it.
in your USercontrol try
Private m_Slots as clsSlots
Public Property Get Slots() as clsSlots
Set Slots = m_Slots
End Property
Gordon
If this wont work then what error are you getting when you try it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That looks right!!
Pretty much what I said!!
The Sooner EE gets binary attachments the sooner we can all post decent sourcecode.
Gordon
Pretty much what I said!!
The Sooner EE gets binary attachments the sooner we can all post decent sourcecode.
Gordon
ASKER
Dalin's answer worked.
caraf_g's anwser is good too, so is Gordonp's comments.
Thanks everyone. I will award each of you with 400 points.
Thanks for the help
caraf_g's anwser is good too, so is Gordonp's comments.
Thanks everyone. I will award each of you with 400 points.
Thanks for the help