First Phase is always looking for example code to allow untangling very poorly written explanations.

Variable Visibility

You can pass variables / objects back and forth between a Visual Basic Form and a UserControl.  This archive has source code showing you how to do this:

     Scribble.zip 

 

You can make properties for the following things: Forms, UserControls and Class Modules. Basically any element that will behave as an object.

When to use properties?

Properties are handy when you want to generalize your code: if you’re changing a setting of something, it might be better to create it as a property. Especially if you might have a need to get a return value as well. The property code can then do some additional stuff, such as switching form’s menu items automatically to correct value, so you don’t need to do it from whereever you set the property.

Property Passing

This accesses a property of the Custom Control

Parent Form Custom Control

 

ParentForm.Caption=UserControl1.Caption  

Dim m_Caption As String

Public Property Get Caption() As String
   Caption = m_Caption
End Property

Custom Control to Parent Form:   You call the property from a form the usercontrol is placed on.  

This Sets a property of the Custom Control

Parent Form Custom Control

 

UserControl1.Caption = "This is my control!"

Dim m_Caption As String

Public Property Let Caption(ByVal NewCaption As String)
    m_Caption = NewCaption
End Property

The difference betweeen Property Let and Property Set  — Property Let is used when you pass a variable. Set is used when you pass an object.

Object Passing

This accesses an object of the Custom Control

Parent Form Custom Control

 

Set ParentForm.Picture = UserControl.Picture

Dim m_Picture As IPictureDisp      ‘ –reference to "Ole Automation" required! by default it is included

Public Property Get Picture() As IPictureDisp
    Set Picture = m_Picture
End Property

This sets an object of the Custom Control

Parent Form Custom Control

 

Set UserControl1.Picture = Picture1.Picture

Dim m_Picture As IPictureDisp

Public Property Set Caption(ByRef NewPicture As IPictureDisp)
    Set m_Picture = NewPicture
End Property

Some notes about the examples

I kept the examples as clean as possible. For this reason, I didn’t include additional coding that would redraw the usercontrol for example. Redrawing would be placed in the property handling as otherwise your control would never change. It is a good idea to make a general redrawing procedure within your usercontrol to keep it visually updated with ease.

Adding it all up

– use a property instead of a sub or function if you need to also get the value
– use a sub if you just need to set something
– use a function if you need to set something and require a return value of the result (such as if the function succeeded)
– Property Get works with any data, be it a regular variable or an object
– Property Let is for regular variables
– Property Set is for objects

You can also specify parameters for property methods

A common example of this is when writing a collection class.

Property Get Item(ByVal Index As Long) As MyClassType
   Set Item = BaseCollection(Index)
End Property

Property Set Item(ByVal Index As Long, ByRef pObject As MyClassType)
   Set BaseCollection(Index) = pObject
End Property

The function signatures (type and number of arguments) for the Get and Set/Let methods must match, save for the additional final parameter on the Set/Let routine which is what is being assigned to the property.

As Merri mentioned, Property Set is called when you assign an object reference using the Set keyword. The object is passed by reference. Property Let is called when you assign a value type (Long, String etc.) optionally using the (deprecated) Let keyword, and it is passed by value.

So, you would use the above property Get/Set methods like this:
Dim AnObject As MyClassType
Set AnObject = MyCollectionClass.Item(5) ‘ Get
Set MyCollectionClass.Item(5) = AnObject ‘ Set

 

It works, but you may notice that the built-in VB Collection class has a slightly neater syntax: 

  • Set AnObject = MyCollection(5)

To accomplish this, you need to set the Item property as the default method of your collection class. To do this, click anywhere in your procedure, and from the Tools menu, select Procedure Attributes. Click Advanced to expand the dialog, and from the Procedure ID dropdown, select (Default). This will enable the use of the shorthand syntax shown above.

Inheritance

 

Hierachical Usage of UserControls

I want to be able to use a UserControl inside of a UserControl in order to be able to build a hierachy through reuse of my work.  However how can I do this?  My initial experiments show a UserControl set inside another UserControl does not have visibility of the higher level controls objects.

General Notes

  1. At the level of the form that holds a custom control you will see that the   UserControl_Initialize()  will run even though you have not hit the go button.
  2. I have not yet found how I can set a property and have the Custom Control recall it without the containing form "reminding" in some manner its identity.  I have a piece of code where there are 32 of the same custom control in a control array.  If I could set a property at design time like a standard control this custom control could know where to look for a PropertyBag. Unfortunately my custom control has resisted remembering any of the properties I defined.

Recalling UserControl Properties

See this article for more on PropertyBags

 

Categories: CodingVisual-Basic

2 Comments

Eric MonsterID Icon Eric · September 9, 2008 at 9:12 pm

Custom controls kick ass… and easy in Visual Studio. Anything I reuse, i.e. status bar that tracks similar info/connection status/data rates is reused over many apps. One of the most useful things to learn w/ similar apps.

webmaster MonsterID Icon webmaster · October 5, 2008 at 9:13 pm

This is a test of editable contents …. can you edit after writing a comment?

Leave a Reply

Your email address will not be published. Required fields are marked *