Active X Controls in VB 6 Sep05 Slide
Active. X Controls in VB 6 Sep-05 Slide: Active. X Controls in VB
What are Active. X Controls? • Custom UI Controls • Which you can design and add to the toolbox • And then use in applications Sep-05 Slide: Active. X Controls in VB
Creating Simple Active. X Controls • • • Start with New Project. . Active. X Control Design/program as if a form Add a project to Group – test it in that Finally, produce a compiled OCX file For example, a simple clock control. . Sep-05 Slide: Active. X Controls in VB
The Clock Control 1 Sep-05 Slide: Active. X Controls in VB
Clock Control 2 • • It will work by having a Timer Which ticks every second And gets system time And displays it in a label Sep-05 Slide: Active. X Controls in VB
Clock Control 3 Sep-05 Slide: Active. X Controls in VB
Clcok Control 4 - Testing Sep-05 Slide: Active. X Controls in VB
Clock Control 5 – Normal exe project added Sep-05 Slide: Active. X Controls in VB
Clock Control 6 – Set Test as Startup Or when you Run it it will start in a browser Sep-05 Slide: Active. X Controls in VB
Clock Control 7 – The control in use Sep-05 Slide: Active. X Controls in VB
Clock control 7 – Give it a name Sep-05 Slide: Active. X Controls in VB
Clock Control 8 – Make. ocx file Sep-05 Slide: Active. X Controls in VB
Clock Control 8 – Adding Control in a new project Sep-05 Slide: Active. X Controls in VB
Active. X Exercise 1 • Make a Clock control like this but. . • Have 3 fields showing hour, minute and second • Use hour() minute() and second() functions Sep-05 Slide: Active. X Controls in VB
Controls with properties • • • Most controls have properties used for design Example background colour of textbox And properties for user input Such as text in a text box Active. X usually need the same Sep-05 Slide: Active. X Controls in VB
Active. X Controls are like Classes (as are forms) • • They have Can set up Sep-05 Slide: Active. X Controls in VB methods private data members property let and get routines events
Two 'users' of custom controls • One user is the programmer who uses the control in a form design • The other is the final end-user who uses that form • You have to write code for both types of user Sep-05 Slide: Active. X Controls in VB
Properties modified at design-time • • Need public property Let and Get and private data member for internal representation But property values differ between control instances and must 'persist' when Form not in memory so must be written to disc somehow actually stored in the. frm file this persistence is not automated Sep-05 Slide: Active. X Controls in VB
Example – a spinner control • Input integer values • Buttons to increase/decrease value • Need a Step. Size property Sep-05 Slide: Active. X Controls in VB
End-user code Private Sub Command 1_Click() Text 1. Text = CInt(Text 1. Text) + Step. Size End Sub Private Sub Command 2_Click() Text 1. Text = CInt(Text 1. Text) - Step. Size End Sub Sep-05 Slide: Active. X Controls in VB
Dim ssize As Integer Public Property Let Step. Size(val As Integer) ssize = val Property. Changed "Step. Size" End Property Designer-user code Public Property Get Step. Size() As Integer Step. Size = ssize End Property Private Sub User. Control_Write. Properties(Prop. Bag As Property. Bag) Prop. Bag. Write. Property "Step. Size", Step. Size, 0 End Sub Private Sub User. Control_Read. Properties(Prop. Bag As Property. Bag) Step. Size = Prop. Bag. Read. Property("Step. Size", 0) End Sub Private Sub User. Control_Initialize() Text 1. Text = 0 End Sub Sep-05 Slide: Active. X Controls in VB
Designer use Sep-05 Slide: Active. X Controls in VB
. FRM contents Sep-05 Slide: Active. X Controls in VB VERSION 5. 00 Object = "*Aspinner. Project. vbp" Begin VB. Form 1. . Scale. Width = 4680 Start. Up. Position = 3 'Windows Default Begin Project 1. User. Control 12 Height = 855 Left = 840. . _Extent. Y = 1508 Step. Size = 9 End Begin Project 1. User. Control 11 Height = 855. . _Extent. Y = 873 Step. Size = 5 End. .
Active. X Exercise 2 • Write a 'slider' control for numeric input • When user drags mouse from area, input is mouse Y • Have textbox and picture. Box • Program the mouse. Move event of the Picture. Box, use the Y parameter • Once working. . add a scale property set at design-time • Then expose a 'Value' property which allows access to the number in the (Let and Get) and read/write Sep-05 Slide: Active. X Controls in VB
Adding Events to controls • • A control can make an event occur This would be a 'custom' event for the control Designer can then program an event-handler for it For example, in the spinner control, could hav ea Bad. Key event if non-digit key pressed. . Sep-05 Slide: Active. X Controls in VB
Control with Event. . Public Event Badkey(). . Private Sub Text 1_Key. Down(Key. Code As Integer, Shift As Integer) If Key. Code = 13 Or Key. Code = 8 Then Exit Sub 'Enter or backspace If Key. Code < 48 Or Key. Code > 57 Then ' not 0 to 9 Raise. Event Badkey End If End Sub Sep-05 Slide: Active. X Controls in VB
Designer writes event-handler Sep-05 Slide: Active. X Controls in VB
Active. X Control Exercise 3 • Add a 2 Click event to the slider control • Triggered when picturebox double-clicked • As designer, use it to zero the value Sep-05 Slide: Active. X Controls in VB
- Slides: 28