Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Advanced
May 11, 1999

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 DBGrid
Dim m_ParentForm As Form

Public Sub SubclassGrid(AnyGrid As DBGrid)
	Set m_InternalGrid = AnyGrid
	Set m_ParentForm = AnyGrid.Parent
End 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:
 
IGridCallback

Public Function GetTableName() As String
End Function
I change my SubclassGrid function in the class to look like this:
 
Dim WithEvents m_InternalGrid As DBGrid
Dim m_ParentForm As Form

Public 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 If
End 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.
Jose Mojica
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date