devxlogo

Subclass Grid Controls

Subclass Grid Controls

Sometimes a class needs to communicate with the object that created it. For example, I use a class to subclass grid controls so I can handle things such as tabbing between columns and unbound population automatically. The class has a SubclassGrid method that makes the subclassing mechanism work, and at the same time saves a copy of the creator object:

 Dim WithEvents m_InternalGrid As DBGridDim m_ParentForm As FormPublic Sub SubclassGrid(AnyGrid As DBGrid)	Set m_InternalGrid = AnyGrid	Set m_ParentForm = AnyGrid.ParentEnd Sub

In some instances, the class needs to get information from the form using the class. To get information from the form, the class can fire an event, or the form can have a Public property, function, or method that the class can call. However, both mechanisms are optional. The creator of the form can forget to write code to handle the events, or forget to add a public function, or even name it differently from what the class expects. To prevent this, I write an IGridCallback interface definition:

 IGridCallbackPublic Function GetTableName() As StringEnd Function

I change my SubclassGrid function in the class to look like this:

 Dim WithEvents m_InternalGrid As DBGridDim m_ParentForm As FormPublic Sub SubclassGrid(AnyGrid As DBGrid)	Dim pIGridCallback As IGridCallback	Set m_InternalGrid = AnyGrid	Set m_ParentForm = AnyGrid.Parent	On Error Resume Next	Set pIGridCallback = m_ParentForm	If Err.Number  0 Then		MsgBox "Your form must implement the " & _			"IGridCallback interface in order to " & _			"use the SubclassGrid class"	End IfEnd Sub

The SubclassGrid routine displays an error message box if it doesn’t find the IGridCallback interface in the form using it. This mechanism guarantees that everyone using the SubclassGrid class must implement the IGridCallback interface. Because implementing the interface means you must support each method in the interface, it also means everyone using the class has the correct functions.

devx-admin

Share the Post: