jwcolby
jwcolby at colbyconsulting.com
Tue Feb 17 11:56:06 CST 2009
Control Labels It is sometimes useful to be able to manipulate the label of a control in some way. For example if a control is going to have a dbl-click event behavior we might want to set the label to a different color so that the user can visually see that they can double click on that combo to modify the list behind the combo. You may find your own reasons to modify the control's label. Each control has a Controls() collection just like the form does. In most of the controls, the Controls collection has nothing but the label in it. In all cases, the label is the index position zero in the collection if the label exists. Not every control has a label, for example tabs and pages. Even controls that do normally have a label can have the label intentionally removed by the developer. Thus while we can in fact obtain a pointer to the label, we may have to do tests to determine whether the label exists. In order to practice building classes, we will build a new clsCtlLbl for our label control. " In the demo database, click Insert / Class. " Immediately save as clsCtlLbl " In the header add the following code: ' Private mlbl As Label 'The label associated with this control ' " In the left combo box, select the class. This will insert the _Initialize event stub which we will not be using. " In the right combo box select Terminate. This will insert the Terminate stub. " Insert the following code in the Terminate stub: Private Sub Class_Terminate() Set mlbl = Nothing End Sub The class Terminate() event performs cleanup for the class. " In mInit add the following code: Function mInit(ctl As Control) mGetLbl ctl End Function This function passes in a control to the label class. mGetLbl ctl will search the control's Control() collection to see if there is a label in the collection, and if so get a pointer to it. " Immediately under mInit() add the following code: Property Get ctlLbl() As Label Set ctlLbl = mlbl End Property This code adds a property to the class which allows the parent class to get a pointer to the label. " In the bottom of the class add the following code ' 'Connects a label to a control - used for continuous forms where the label is in the header etc. ' Function ConnectLabel(llbl As Label) Set mlbl = llbl End Function ' 'Finds the label that "belongs to" the passed in control. ' Private Function mGetLbl(ctlFindLbl As Control) As Label Dim ctl As Control For Each ctl In ctlFindLbl.Controls If ctl.ControlType = acLabel Then Set mGetLbl = ctl Exit For End If Next ctl Exit_mGetLbl: Exit Function End Function In this lecture we have discussed why we might want to manipulate the label of a control. We then created a new class to hold all of the data and code required to process labels, and to get a pointer to the label. -- John W. Colby www.ColbyConsulting.com