ActiveX Control Tutorial - Part 4
Now I don't know about you, but I ain't awfully satisfied with my property page. It looks about as stunning as a sumo wrestler's bottom.
So let's try to spruce it up a little by adding support for our custom AcceptType and ConvertCase properties:
- Add two Labels and two Combo Boxes to your Property Page
- Name the Combo Boxes 'cboAcceptType' and 'cboConvertCase' respectively
- Change both their Style properties to '2 - Dropdown List'
Now, when the property page opens, we want those two Combo Boxes to hold our enumeration options such as 'All' or 'Letters'. Unfortunately there isn't an easy way to automatically do this, so we'll add them manually under the property page's Initialize event (similar to Form_Load).
- Add the following code to your property page:
Private Sub PropertyPage_Initialize() With cboAcceptType .AddItem ("Anything") .AddItem ("Just Numbers") .AddItem ("Just Letters") End With With cboConvertCase .AddItem ("Anything") .AddItem ("Upper Case") .AddItem ("Lower Case") End WithEnd Sub
Try testing your project now. When you open the property page, are you able to select an item from the list?
Good let's continue:
- In the Click event behind each Combo Box, add the code:
Remember, this tells Visual Basic that at least one property on the page has been changed.
Now let's add a little code to the bit which reads properties and sets the value of your Text Boxes, etc.
- Add the following code to the SelectionChanged event:
Select Case SelectedControls(0).AcceptType' Note that I'm using the enumeration name prefix' instead of the ambiguous "All"Case CharacterType.AllcboAcceptType.Text = "Anything"Case CharacterType.LetterscboAcceptType.Text = "Just Letters"Case CharacterType.NumberscboAcceptType.Text = "Just Numbers"End SelectSelect Case SelectedControls(0).ConvertCaseCase CaseType.AllcboConvertCase.Text = "Anything"Case CaseType.LowerCasecboConvertCase.Text = "Lower Case"Case CaseType.UpperCasecboConvertCase.Text = "Upper Case"End Select
Here, we're just examining the current AcceptType and ConvertCase properties, then changing the Combos as appropriate.
So that's how we get the initial properties into the property page. Now we need to deal with how to get them out, after the user clicks OK or Apply.
- Add the following code to the ApplyChanges event:
Select Case cboAcceptType.TextCase "Anything"SelectedControls(0).AcceptType = _ CharacterType.AllCase "Just Letters"SelectedControls(0).AcceptType = _ CharacterType.LettersCase "Just Numbers"SelectedControls(0).AcceptType = _ CharacterType.NumbersEnd SelectSelect Case cboConvertCase.TextCase "Anything"SelectedControls(0).ConvertCase = _ CaseType.AllCase "Lower Case"SelectedControls(0).ConvertCase = _ CaseType.LowerCaseCase "Upper Case"SelectedControls(0).ConvertCase = _ CaseType.UpperCaseEnd Select
Here, we're just doing the reverse of the SelectionChanged event. We're analysing the content of the two Combo Box controls, then changing the user's control properties to reflect them.
Top Tip: There are numerous ways in which this code can be improved, but has been left as shown for simplicity. Can you think of a different method to set our properties other than via text comparison in a Select Case statement? Can you see where you could use the With keyword?
Go ahead; test your property page in the usual manner. Does it work? Notice how changes made in the Properties window are instantly reflected in your property page, and vice versa. That's all down to the mysterious 'PropertyChanged' command, keeping everyone up-to-date as to the latest happenings.
Phew! What a difficult section!
But it's time to celebrate congratulations on getting this far! You've successfully completed all the main components of the Super Cool Text Box control!
<Karl gives you a big kiss>
Note: A full download of the Super Cool Text Box control is available here.
Page 7 of 8