This walkthrough describes how to build two DTCs that share information using the Choices interface. For simplicity, one DTC acts solely as a publisher and shows how a choice can be published. The other DTC is the subscriber that shows how a choice can be read and used by another DTC. Keep in mind that the ultimate purpose of the DTCs is to generate run-time text for use in a Web page.
The publishing DTC user interface has a label and a drop-down list. When a user chooses a value from the drop-down list, the DTC publishes that value as a choice to a design-time control site and generates run-time text showing the selected value. The subscribing DTC user interface has a label. The DTC uses a ChoiceSink to get the information published by the other DTC and sets the caption for that label based on the value of the choice that was published. This inline user interface is used in the sample; for your DTCs you should use property pages instead.
For working DTCs, see VBSample.ocx provided with this SDK. For more information, read Sharing Information Between DTCs or the Choices Reference.
The main tasks for publishing choices are:
To begin an ActiveX control project
Tip Rename the project and the user control. Although renaming is not required, changing the names makes it easier to find the control when adding it to the Visual InterDev toolbox.
Detail From the Project menu, choose References. In the References dialog box, locate and select the following libraries. By default, these libraries are located in the Include directory provided with the SDK.
File Name | Reference Display Name |
DTC60.tlb | Microsoft DTC 60 Type Library |
webdc.tlb | Microsoft Web Design-time Control Type Library |
Note When creating a DTC, use property pages instead of inline user interface for setting a DTC's properties. An inline user interface is the graphical representation of the DTC within the hosting document such as a form or other object with controls that accept user input. Although Visual InterDev 6.0 supports inline user interface, this is not a guarantee that all DTC hosts are required to support it now or in the future.
An IDesignTimeControlSite works with a DTC to act as a clearinghouse for choices. Through its objects and methods, you can store and access information published and subscribed to by multiple DTCs. The interface site points to the methods for choice objects. A Choice object stores a value that a DTC publishes so that the site is able to pass it to other DTCs. The hosting editor sets the DesignTimeControl property when the DTC is created.
To implement the property
Implements IProvideRuntimeText
Implements IDesignTimeControl
Dim MySite As IDesignTimeControlSite
Note If you use the Code window's textboxes to automatically generate the properties, you may want to change the variable from "RHS" to "Site" for clarity and to match the sample.
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As DTC60.IDesignTimeControlSite)
End Property
Private Property Get IDesignTimeControl_DesignTimeControlSite() As DTC60.IDesignTimeControlSite
End Property
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As DTC60.IDesignTimeControlSite)
Set MySite = Site
End Property
Now you are ready to add the user interface and publish a choice.
After you have specified a site, you can publish a choice. To see a sample containing a publishing DTC, see the Fruit sample, SourceDTC.
For example, if you were creating the Fruit sample, you would enter the following:
Private Sub UserControl_Initialize()
With Combo1
.AddItem "Apple"
.AddItem "Banana"
.AddItem "Cherry"
.AddItem "Durien"
End With
End Sub
Dim MyChoice As Choice
Note The type property of the choice object is the key value that subscribing DTCs use to determine whether or not to use choice. In addition, you can add tags to a choice and subscribing DTCs can also use those for evaluating a choice.
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As DTC60.IDesignTimeControlSite)
Set MySite = Site
Set MyChoice = MySite.Choices.AddChoice(Nothing, Combo1.Text, "MyChoiceType")
End Property
Note Visual Basic requires that all of the methods of the interface be mentioned in the code whether they have code specified or not. The Code window indicates methods you have added in boldface. You need to add those that are not in boldface in order to compile.
In the sample, those methods are:
Now that the choice has been published, you can make sure that your DTC is able to recall the value for the choice.
Your DTC needs to persist the choice it was publishing so that same choice appears in the combo box when the page is reopened. If your run-time text depends on a choice, you need to persist that value for reloading even if the Choice object no longer exists.
To persist the choice value in a property bag
Public Property Get MyStorageProp() As String
MyStorageProp = Combo1.Text
End Property
Public Property Let MyStorageProp(ByVal New_MyStorageProp As String)
Combo1.Text = New_MyStorageProp
PropertyChanged "MyStorageProp"
End Property
Private Sub UserControl_ReadProperties(MyPropBag As PropertyBag)
Dim sRead As String
Dim s As String
Dim I As Integer
sRead = MyPropBag.ReadProperty("MyStorageProp", "")
For i=0 To Combo1.ListCount-1
If Combo1.List(I) = sRead Then
Combo1.ListIndex = I
Exit For
End If
Next
End Sub
Private Sub UserControl_WriteProperties(MyPropBag As PropertyBag)
Call MyPropBag.WriteProperty("MyStorageProp", Combo1.Text, "")
End Sub
The properties and a property bag store the choice values for passing between DTCs. When you bind to a choice you need to persist that value for reloading the information even if the choice no longer exists.
Private Sub Combo1_Click()
If Not MyChoice Is Nothing Then
MyChoice.Description = Combo1.Text
PropertyChanged "MyStorageProp"
End If
End Sub
Now that your DTC has its user interface and published choice defined, you can specify the run-time text that the DTC inserts into the document.
The final output of the DTC is the run-time text that you want inserted into the Web page. In Visual Basic, implement the GetRuntimeText method of the IProvideRuntimeText interface.
To specify the run-time output
Private Function IProvideRuntimeText_GetRuntimeText() As String
Dim s As String
s = "<B>Publishing the choice " & Combo1.text _
& "!</B>"
IProvideRuntimeText_GetRuntimeText = s
End Function
Before you can test the DTC in a hosting editor such as Visual InterDev, you need to register it. There are two ways to register a DTC created in Visual Basic. The procedure below describes the recommended way for testing your DTC. For more information, see Registering a Visual Basic Control for Testing. For the recommended procedure for registering a DTC ready for deployment and distribution, see Distributing a DTC.
To register your DTC
How to From the File menu, choose Make. In the Make Project dialog box, specify the directory you want the .ocx to reside in, then choose OK.
How to From the Windows taskbar, choose Start and then Run. Type Regsvr32 and the path to the .ocx. A message box appears when the .ocx is registered.
How to From the Windows taskbar, choose Start and then Run. Type Regsvrdc followed by the ProgID of the control.
For example, you would enter the following if you were using this procedure on the SrcDTC in the VBSample project.
Regsvrdc vbsample.SourceDTC
You are now ready to test the code you've written by using the DTC inside Visual InterDev6.0. If you make changes to the DTC after viewing it in Visual InterDev, you need to close and then reopen Visual InterDev before testing the modified control.
To test your DTC code
Note To add the control, right-click the toolbox and choose Customize Toolbox. In the dialog box, choose the ActiveX Controls tab and locate your DTC.
Note If you don't see the run-time text update after you change a value, click on another area of the page to update the run-time text view.
For more information about testing, see Testing a DTC's Output.
For the purposes of this walkthrough, the subscribing DTC has a single label for displaying the published choice. Like the publishing DTC, the subscribing DTC requires a site. However, only the subscribing DTC requires a ChoiceSink.
The main tasks for creating the subscribing DTC are:
For more information about subscribing, see Subscribing to a Choice.
The following procedure assumes you have already created your ActiveX control project and the publishing DTC as described previously.
Implements IProvideRuntimeText
Implements IDesignTimeControl
Dim MySite As IDesignTimeControlSite
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As DTC60.IDesignTimeControlSite)
Set MySite = Site
End Property
Private Property Get IDesignTimeControl_DesignTimeControlSite() As DTC60.IDesignTimeControlSite
If Not MySite Is Nothing Then
Set IDesignTimeControl_DesignTimeControlSite = MySite
End IF
End Property
The site object uses the ChoiceSink object to evaluate which published choices are of interest to the subscribing DTC. For more information about using a sink, see Identifying a Choice of Interest
Warning The ChoiceSink type property must match the published Choice's type property. If the two are not the same, the subscribing DTC does not evaluate it as a usable choice.
Dim MyChoiceSink As ChoiceSink
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As DTC60.IDesignTimeControlSite)
Set MySite = Site
Set MyChoiceSink = MySite.ChoiceSinks.AddChoiceSink("MyChoiceType")
End Property
Private Sub IDesignTimeControl_OnChoiceChange(ByVal cs As ChoiceSink, ByVal Change As DTC60.dtcChoiceChange)
If cs Is MyChoiceSink Then
Dim ch As Choice
Set ch = MyChoiceSink.BoundChoice
If Not ch Is Nothing Then
Label1.Caption = ch.Description
Else
Label1.Caption = ""
End If
End If
End Sub
In the sample, those methods as displayed in the Code window text box are:
Public Property Get SubscribedValue() As String
SubscribedValue = Label1.Caption
End Property
Public Property Let SubscribedValue(ByVal New_SubscribedValue As String)
Label1.Caption = New_SubscribedValue
PropertyChanged "SubscribedValue"
End Property
Private Sub UserControl_ReadProperties(MyPropBag As PropertyBag)
Label1.Caption = MyPropBag.ReadProperty("SubscribedValue", "")
End Sub
Private Sub UserControl_WriteProperties(MyPropBag As PropertyBag)
Call MyPropBag.WriteProperty("SubscribedValue", Label1.Caption)
End Sub
The final steps for creating and testing your subscribing DTC are the same as those for your publishing DTC. They are summarized here using the sample subscribing DTC. For more information about testing a DTC, see Testing a DTC's Output.
Private Function IProvideRuntimeText_GetRuntimeText() As String
Dim s As String
s = "<P><B>The Web author selected: " + Label1.Caption + ".in the publishing DTC.</B></P>"
IProvideRuntimeText_GetRuntimeText = s
End Function
Warning If you make changes to the publishing DTC after viewing it in Visual InterDev, you need to reopen Visual InterDev before testing the modified control. Otherwise the old version of the control is used by Visual InterDev.
Note To add the control, right-click the toolbox and choose Customize Toolbox. In the dialog box, choose the Design Time Controls tab and locate your DTC.
Note If you don't see the run-time text updated after you change a value, click on another area of the page to update the run-time text view.
You can verify that choices are being published by adding the Choice Inspector to the page. For more information about using this tool, see Viewing Available Choices.