View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Simon Murphy[_2_] Simon Murphy[_2_] is offline
external usenet poster
 
Posts: 45
Default Is this the right way to implement your own VBA Control?

Jim
This is the containment, perfectly valid and the approach I tend to use.

You could use interface inheritance (Implements keyword)(this is how you
descend from another class in VBA) but then you have to define each
method and explicitly call the parents implementation or code your own.
I have never found it as useful as the full inheritance provided by
other languages.

Cheers
Simon
Excel development website: www.codematic.net


Jim Luedke wrote:
If you want to improve a Control's functionality, how do you do it?

The code below is based on my hunch of how VBA works. Amazingly, it
works. But is it legit?

My UserForm has a TreeView. In Initialize(), I equivalence it to a New
instance of my "ImprovedTreeView" class.

Is that the best, most VBA-native way to do it? Even eliminating one
step would improve things.

My biggest confusion is: In a class module, how do you descend from
another class?

I want CImprovedTreeView to have all the behavior of TreeView, plus my
stuff.

I get around it now by implementing a property of CImprovedTreeView of
the type (TreeView) I want to descend from. But that sure is kluge-a-
delic:

***

‘CLASS MODULE CImprovedTreeView

Option Explicit

Private g_Tree As TreeView

'Sample property...which works fine!

Public Property Get FirstNode() As Node
'Action: Return the topmost node of the tree.
'Note: I was going crazy, because I thought, doesn't .Nodes(1) give
you that? Nein, Dummkopf!

Dim N As Node

'Take any node, ascend to its root, then take the root's first
sibling.
With g_Tree
If .Nodes.Count 0 Then
Set N = .Nodes(1)
While Not (N.Parent Is Nothing)
Set N = N.Parent
Wend
Set FirstNode = N.FirstSibling
End If
End With
End Property

Public Property Set Tree(TV As TreeView)

Set g_Tree = TV
End Property


‘MODULE UserForm1
‘TreeView1 is on this form.

Option Explicit

Private ImprovedTreeView as CImprovedTreeView

Private Sub UserForm_Initialize()
‘Note: Why do I have to get involved with naming two classes (TreeView
and CTreeView)? Can I dispense with one?

Set ImprovedTreeView = New CImprovedTreeView
Set ImprovedTreeView.Tree = TreeView1
End Sub

Private Sub DoSomething
Dim N as Node

Set N = ImprovedTreeView.FirstNode
‘etc.
End Sub

***

Thanks.

***