Adding Properties to Our Control

Visual Studio 6.0

Now we're going to add some properties to our control. The easiest way is to let VB do the grunt work of adding the property templates for us.

Try It Out - Adding a few Properties to the pubCtl

1.  Click on the control form to bring up the code window. Then from the main VB menu, select Tools-Add Procedure… to bring up the Add Procedure window. We want to add some public properties so that they can be accessed from outside of our control. When we select Property as the type of procedure, VB is kind enough to add both a Let and Get property for us:

2.  Add three new public properties, called company, name and id. Because these are just templates that are added (it is up to us to add the code to make the control do something) VB puts in default values for parameters and return values. Please take a look at what VB puts in and change the values accordingly to those shown below.

3.  Be sure to change the default names of the parameters of the properties and to change the default types of the parameters from Variant to String for the txtName and txtCompany text boxes' Get properties. Change the return type to String from Variant as well. Change the txtPubID data types from Variant to Long.

Public Property Get company() As String
company = txtCompany
End Property
Public Property Let company(ByVal newCompanyName As String)
txtCompany = newCompanyName
End Property
Public Property Get name() As String
name = txtName
End Property
Public Property Let name(ByVal newName As String)
txtName = newName
End Property
Public Property Get id() As Long
id = txtPubID
End Property
Public Property Let id(ByVal newPubID As Long)
txtPubID = newPubID
End Property

How It Works

Notice that we have both a Get and a Let for the company, the name and the id properties. The Gets are responsible for retrieving the values of the txtCompany, txtName and txtPubID text boxes and assigning the values to our private variables. Likewise, the Lets are responsible for assigning the values from the new record to our text boxes. When the data repeater needs to display a new record, it simply invokes the Let properties of the text boxes and displays the new values as the user scrolls through the recordset. So by using the data repeater, our class is made pretty simple. For each text box we have in our control, we simply write code for the Change event of each text box. Then we write a Let and Get for each text box and we are finished with the class. Voila! The data repeater does all of the grunt work of retrieving and displaying the correct fields of the correct record in each instance of our class displayed.

© 1998 by Wrox Press. All rights reserved.