ChiefRedBull
09-29-2001, 08:43 AM
Howdy.
I am looking into writing my own OCX controls - ie a better progress bar, custom buttons.
Could someone explain the way I use the Let and Get things in my ".ctl".
I have downloaded stuff off PSC but i want to understand the code so i can write my own.
Any help greatly appreciated. images/icons/wink.gif
Chief
"How are we to learn, if those that know will not teach... ?" - Me.
Derek Stone
09-29-2001, 09:25 AM
Get will retrieve a property from the control, while Let will set the property of a control.
Set is used when you are dealing with objects. In most cases Set won't be used.
Example:
<pre>Public Property Get Enabled() As Boolean
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
m_Enabled = New_Enabled
PropertyChanged "Enabled"
End Property
</pre>
Good Luck
-cl
Squirm
09-29-2001, 09:27 AM
You use Public Property Let to allow things to be set (like a Caption) and Public Property Get to return the value.:
<pre>Public Property Let Enabled(bEnabled As Boolean)
If mEnabled <> bEnabled Then
mEnabled = bEnabled
UserControl_Paint
End If
End Property
Public Property Get Enabled() As Boolean
Enabled = mEnabled
End Property</pre>
For this, you would need a variable mEnabled within your control.
ChiefRedBull
09-29-2001, 10:03 AM
I thought so - just needed that cleared up. images/icons/wink.gif
Muahaha the world is mine!!
Cheers
Chief
"How are we to learn, if those that know will not teach... ?" - Me.
ChiefRedBull
09-29-2001, 02:57 PM
Okley Dokley.
Got that bit working fine - now..
For exmaple - how could I change the "Enabled" property, from within the "BackColor" property setting sub? Is this possible?
The other thing - how do i set the control to resize properly when the user is drawing it onto the form?
Thanks a bunch images/icons/wink.gif
Chief
"How are we to learn, if those that know will not teach... ?" - Me.
Derek Stone
09-29-2001, 03:53 PM
Notify that a property has changed:
<pre>
m_Enabled = False
PropertyChanged "Enabled"
</pre>
As for the resizing just place your code in the UserControl_Resize event.
Example:
<pre>Private Sub UserControl_Resize()
<font color=green>'The control shouldn't be any higher than 10 pixels (about 150 twips)</font color=green>
UserControl.Height = 10 * Screen.TwipsPerPixelY
<font color=green>'Refresh the control to make sure changes show up on screen</font color=green>
UserControl.Refresh
End Sub
</pre>
Good Luck
-cl
Squirm
09-29-2001, 05:12 PM
You can change anything you like from within any property let/get sub.... in my example above mEnabled is a variable within the control. You can set it within another set if you wish:
<pre>Public Property Let BackColor(lColor As Long)
If BackColor = vbBlack Then
mEnabled = True
Else
mEnabled = False
End If
mColor = lColor
Usercontrol_Paint
End Property</pre>
ChiefRedBull
09-30-2001, 04:00 AM
Do you guys always post in pairs? LoL.
Right. Easy then huh? Thankyou, thankyou.
images/icons/wink.gif
Chief
"How are we to learn, if those that know will not teach... ?" - Me.
Squirm
09-30-2001, 04:16 AM
You might also want to look into the PropBag which allows you to set your properties within the IDE, in the Properties window.
<pre>Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
mCap = PropBag.ReadProperty("Caption", UserControl.Name)
mEnabled = PropBag.ReadProperty("Enabled", True)
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "Enabled", mEnabled, True
PropBag.WriteProperty "Caption", mCap, UserControl.Name
End Sub</pre>
Basically this allows you to get and set properties from the IDE properties window. With WritePropery, you specify the name of the property in the IDE, the variable to take the value from, and the default value. With ReadProperty, you do the same.
Although they are not essaential, being able to edit your control in the IDE is a real useful thing. Properties such as height and width are always included and you dont need to read/write them in this way.
Good luck images/icons/wink.gif
Squirm
09-30-2001, 04:18 AM
Actually - I think you might benefit from downloading The Hand's hovbutton control, just to look into the code and see how such things work. It is very well written and will really help you get to grips with UserControls.
ChiefRedBull
09-30-2001, 04:35 AM
Thanks Squirm, I'll do that.
images/icons/smile.gif
Chief
"How are we to learn, if those that know will not teach... ?" - Me.