Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

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.



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