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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS