Title: ActiveX%20Controls,%20Enumerated%20Constants%20
1ActiveX Controls, Enumerated Constants With
Events and Other VB Stuff
- Pat Tormey PE
- Foursquare Solutions Inc.
- www.foursquare.com
2How It Looks
One Control, With Several Methods
3The Users Design View
Added to Your Tools
AND Dropping Down in the Property Box
4Enums on the User Form
'This Enum is for this form ONLY Private Enum
Display Green 0 Yellow 1 Red 2 StopSign
3 End Enum Display
Enums give you Free Type-ahead
Private Sub cmd_Click(Index As Integer) On Error
Resume Next Select Case Index Case
Display.Green You get Type-a-Head
uscTrafficLight1.Lights Green .. Case
Display.StopSign uscTrafficLight1.Lights
StopSign End Select End Sub
5Building The Control (I)
Add a New UserControlDrop in Four image Boxes
in a Control Array called Img()They are
all invisible but one...
Name it something you can remember.
6Building the Control (II)
Stack them all on top of each otherAnd make the
User Control as small as you can
Youll Also want to select a ToolBox Bitmap
property Its on this list...
7Enums in the Control
Private miFlag As Integer Public Enum Display
Note its Public Here Green 0 Yellow 1
Red 2 StopSign 3 End Enum 'Public Public
Property Let Show(Flag As Display) 'Making this
PUBLIC causes it to show up in the control's
Property's Window as WRITE miFlag Flag
Lights Flag Pass it thru to the Lights Method
End Property
As a parameter Enums Limit the users options
Less error checking...
Code Behind The UserControl
8Public Methods
Public Property Get Show() As Display 'Making
this PUBLIC causes it to show up in the
control's Property's Window as READ. Show
miFlag End Property
A Public Method It shows up in the Object Browser
Public Sub Lights(Flag As Display) Dim i As
Integer 'The trick here is to be sure only one
light is visible at a time For i 0 To 3
img(i).Visible (Flag i) Next End Sub
9In the Object Browser
Be sure to add a descriptionIt shows in the
Browser
10Raising Events
- Your Control can raise an event that will be
seen by its parent form - Classes can also raise events the same way but
you must declare them With Events and you can
not use the New keyword as part of the
declaration - Heres a simple example...
11The Change of State Event
The Event Change of StateIs Passed to each
forms Icon Property
12More Event Stuff
It Even Shows up on the ToolBar
13Creating a PUBLIC Event in the Control
Here is how its done...
Private miFlag As IntegerModule Wide Scope to
share the State
Public Event ChangeState(miFlag as integer) 'If
the state Changes, it lets the world Know Public
Property Get Icon() Just returning the Selected
Icon Set Icon img(miFlag).Picture End Property
14Raise the Event in Controls Code
Public Sub Lights(State as Integer) Dim i As
Integer On Error Resume Next For i 0 To 3
img(i).Visible (Flag i) Next miFlag Flag
So it can be passed back RaiseEvent
ChangeState(miFlag) End Sub
15The Users View of the Event
Once added to the Users Form, the Control has
YOUR new Event. And Carries the Parameter you
assigned!
Private Sub uscTrafficLight1_ChangeState(State as
Integer) Set Icon uscTrafficLight1.Icon
Set Form2.Icon uscTrafficLight1.Icon Set
Form3.Icon uscTrafficLight1.Icon Set
Form4.Icon uscTrafficLight1.Icon End Sub
16Foursquare Solutions Inc.
- www.foursquare.com
- Pat Tormey