View contents of this chapter

Creating ActiveX Controls

If you find yourself making the same kinds of changes to VB controls to get them to operate the way you need, consider writing new controls that have built in properties and methods that do what you need them to do.

When you write an application that needs your control, add your own control to the toolbox and set the properties you want to set.

Now we will create a very simple control that shows the current time. The source code needed for this example is stored in the "Source Code\Chapter22\Time" directory.

Select New, Project to see the New Project dialog box, select the ActiveX Control icon. Your screen will look like the one shown in the following figure.

Private Sub Timer1_Timer()
  lblTime.Caption = Time
End Sub

Private Sub UserControl_Resize()
  ' set the control to the width and height
  If UserControl.Height <> lblTime.Height Then
    lblTime.Height = UserControl.Height
  End If
  lblTime.Move 0, 0, UserControl.ScaleWidth
End Sub

The above example uses a label control and a timer control. The label control shows the current time. The timer fires after every second and updates the time shown by the lblTime control. Run the application, you will see the following:




© Universal Teacher Publications        INDEX Previous Screen Next Screen