ActiveX Controls


Let’s move on from basic COM objects to ActiveX controls. You’re going to do pretty much the same you did with the basic COM component (apart from late binding, which has no relevance to ActiveX controls) - build a legacy control using VB6 and then import it into your .NET Visual Basic project.

The Legacy ActiveX Control

For your legacy ActiveX control, you are going to build a simple buttonlike object that is capable of interpreting a mouse click and can be one of two colors according to its state. To accomplish this task, you will take a second foray into VB6, so if you don’t have VB6 handy, feel free to skip the next section, download the OCX file, and pick it up when you start developing your .NET application.

Step 1: Creating the Control

This time, within the VB6 IDE, you need to create an ActiveX Control project. For this example, call the project Magic, and the control class MagicButton, to reflect its remarkable powers. From the Toolbox, select a Shape control and place it on the UserControl form that VB6 provides for you. Rename the shape provided on the form to shpButton, and change its properties as follows:

Open table as spreadsheet

Property

Value

FillStyle

0 - Solid

Shape

4 - Rounded Rectangle

FillColor

Gray (&H00808080&)

Add a label on top of the Shape control and rename it to lblText. Change this control’s properties to the following:

Open table as spreadsheet

Property

Value

BackStyle

0 - Solid 0Transparent

Alignment

2Center

Switch to the code view of the MagicButton component. Within the code presented, add two properties called Caption and State, and an event called Click(), as well as code to handle the initialization of the properties and persisting them, to ensure that the shape resizes correctly and that the label is centered. You also need to handle mouse clicks within the code. The final code of the MagicButton class should look as follows:

  Option Explicit Public Event Click() Dim mintState As Integer Public Property Get Caption() As String   Caption = lblText.Caption End Property Public Property Let Caption(ByVal vNewValue As String)   lblText.Caption = vNewValue   PropertyChanged ("Caption") End Property Public Property Get State() As Integer   State = mintState End Property Public Property Let State(ByVal vNewValue As Integer)   mintState = vNewValue   PropertyChanged ("State")   If (State = 0) Then     shpButton.FillColor = &HFFFFFF&   Else     shpButton.FillColor = &H808080&   End If End Property Private Sub UserControl_InitProperties()   Caption = Extender.Name   State = 1 End Sub Private Sub UserControl_ReadProperties(PropBag As PropertyBag)   Caption = PropBag.ReadProperty("Caption", Extender.Name)   State = PropBag.ReadProperty("State", 1) End Sub Private Sub UserControl_WriteProperties(PropBag As PropertyBag)   PropBag.WriteProperty "Caption", lblText.Caption   PropBag.WriteProperty "State", mintState End Sub Private Sub UserControl_Resize()   shpButton.Move 0, 0, ScaleWidth, ScaleHeight   lblText.Move 0, (ScaleHeight - lblText.Height) / 2, ScaleWidth End Sub Private Sub lblText_Click()   RaiseEvent Click End Sub Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, _                                 X As Single, Y As Single)   RaiseEvent Click End Sub 

If you build this, you’ll get an ActiveX control called Magic.ocx.

Step 2: Registering Your Legacy Control

You now have your legacy control. As before, if you’re developing your new .NET application on the same machine, then you don’t need to do anything more, because your control will already be registered by the build process. However, if you’re working on an entirely new machine, you need to register it there. As before, open a command window and register it as follows:

 regsvr32 Magic.ocx

Having done that, you’re ready to build your .NET application.

A .NET Application, Again

This .NET application is even more straightforward than the last one. All you’re going to do this time is display a button that changes color whenever the user clicks it. To begin, create a .NET Windows Application project in Visual Basic called ButtonApp. Before you start to develop it, however, extend the Toolbox to incorporate your new control by selecting Tools image from bookChoose Toolbox Items. Figure 23-11 shows the resulting dialog.

image from book
Figure 23-11

When you click the OK button, you can see that your magic button class is now available to you in the Toolbox (see Figure 23-12). Add the Magic.MagicButton control to your form, as shown in Figure 23-13, by checking the box next to the control name. Note that references to AxMagic and Magic are added to the project in the Solution Explorer window within the References folder, as shown in Figure 23-14.

image from book
Figure 23-12

image from book
Figure 23-13

image from book
Figure 23-14

All you need to do now is initialize the Caption property to ON, change the Text of the form to Button Application, and code up a handler for the mouse Click event:

 Private Sub AxMagicButton1_ClickEvent(ByVal sender As Object, _             ByVal e As System.EventArgs) Handles AxMagicButton1.ClickEvent     AxMagicButton1.CtlState = CType(1 - AxMagicButton1.CtlState, Short)     If (AxMagicButton1.CtlState = 0) Then         AxMagicButton1.Caption = "OFF"     Else         AxMagicButton1.Caption = "ON"     End If End Sub

Note something slightly peculiar happening here. In the course of importing the control into .NET, the variable State mutated into CtlState. This is because there is already a class in the AxHost namespace called State, which is used to encapsulate the persisted state of an ActiveX control.

Trying It All Out, Again

What happens when you run this application? Notice the control in the ON position, as presented in Figure 23-15. If you click the control, it changes to the OFF position, as shown in Figure 23-16.

image from book
Figure 23-15

image from book
Figure 23-16




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net