ActiveX Control Tutorial - Part 4, Page 6
Open up the property page our Wizard created. It should look something like this:
Huh, not very exciting. In fact, you're looking at the only computer screenshot so plain it ships with its own aviation certificate.
Let's take a peek at some of the code behind this rather dull property page.
Underneath all the Change events of the Text Boxes and the Click events of the Check Boxes, you should find this code:
Changed = True
This tells Visual Basic that at least one property on the page has changed. Of course, it doesn't signify that the change should be saved - you mustn't forget that all property pages have an OK, Cancel and Apply button. It simply informs VB that a property has been altered a little like the PropertyChanged method of your control.
Now, just as a regular Form has its own events, so does the Property Page. When someone hits the OK or Apply buttons, the ApplyChanges event runs then it's up to you to save the properties. This is a little like the WriteProperties event of your control.
Likewise, when someone first opens your Property Page, the SelectionChanged event fires. This can be compared to the ReadProperties event of your control it allows you to 'set' the various whatnots on your property page.
So let's take a peek at some of the code the wizard threw behind those two events:
Private Sub PropertyPage_ApplyChanges() SelectedControls(0).Text = txtText.Text SelectedControls(0).PasswordChar = txtPasswordChar.Text ' ... etc ... ' This is like WriteProperties - ' It passes the changes on your ' property page back to the control ' SelectedControls(0)End SubPrivate Sub PropertyPage_SelectionChanged() txtText.Text = SelectedControls(0).Text txtPasswordChar.Text = SelectedControls(0).PasswordChar ' ... etc ... ' This is like ReadProperties - ' It reads the properties of your control ' via SelectedControls(0) and displays ' the info via your property page text boxes, ' check boxes, combos, etc.End Sub
Now, you've probably noticed 'SelectedControls' appearing once or thrice throughout the code. This is a method of your property page - basically a direct 'link' to the running instance of your control.
So if you wanted to check out a property of your control, say the PasswordChar value, you can access it via the SelectedControls(0) item. You can read any of our control properties using this format:
... passes back the value of the controls PasswordChar property. The SelectedConrols(0) bit here is just a gateway to your control and its properties.
And if you can understand that, the code should look simple.
Excellent! Let's summarise:
In the ApplyChanges event, you're simply setting properties of the user's control dependant on the various Text Boxes, Check Boxes, etc. present on your property page. You're applying changes made in the property page direct to the control.
In the SelectionChanged event, which remember is similar to our control's ReadProperties event, you merely set the values of your Text Boxes, Check Boxes, etc. dependant on the properties of the user's control.
Put simply, the SelectedControls(0) statement just gives you direct access to the user's instance of your control and all it's properties.
Try testing your control! Add it to a demo project, right click and select Properties. Try changing a few of the options, then click Apply or OK.
Notice how the Properties window updates itself?