Skip to main content

Past Blast

Featured Products

Stay in touch using the DEVBUSS RSS feeds.
 

News

Building a GUI Engine with Frames

Written by Christopher Tacke  [author's bio]  [read 42183 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

So what we need are a stack and methods to push Frames to and pop Frames from that stack. Add a Module to your project for all of your Frame stack code then add the following code:

Option Explicit

Private Const FRAME_X_OFFSET = 45
Private Const FRAME_Y_OFFSET = 5
Private m_objFrameStack() As Frame
Private m_iFramesOnStack As Integer

Public Function PushFrame(objForm As Form, _
ByVal strFrame As String) _
As Long

Dim intIndex As Integer
Dim intPut As Variant
Dim intEmptyFrame As Integer
Dim objThisFrame As Frame
Dim intTopFrame As Integer

' get a reference to the new frame
Set objThisFrame = objForm.Controls(strFrame)

' first frame added
If IsEmpty(m_iFramesOnStack) Then
m_iFramesOnStack = 0
End If

' resize the stack array
ReDim Preserve m_objFrameStack(m_iFramesOnStack)

' store reference to current frame
Set m_objFrameStack(m_iFramesOnStack) = _
objThisFrame

' move the frame to the visible position
m_objFrameStack(m_iFramesOnStack).Left = _
FRAME_X_OFFSET
m_objFrameStack(m_iFramesOnStack).Top = _
FRAME_Y_OFFSET
m_objFrameStack(m_iFramesOnStack).ZOrder 0

' show the frame
m_objFrameStack(m_iFramesOnStack).Visible = True

' hide the frame's border
m_objFrameStack(m_iFramesOnStack).BorderStyle = _
vbBSNone

' increment the stack counter
m_iFramesOnStack = m_iFramesOnStack + 1

' refresh the form
objForm.Refresh
End Function

Public Function PopFrame(objForm As frmMain, _
ByVal intFramesToPop As Integer) _
As Long

' don't pop more than frames than we have
If m_iFramesOnStack - intFramesToPop <= 0 Then
Exit Function
End If

'make frame hidden before popping
m_objFrameStack(m_iFramesOnStack - 1).Visible = _
False

' drop the number of frames desired
ReDim Preserve m_objFrameStack(m_iFramesOnStack - _
intFramesToPop - 1)

' decrement counter
m_iFramesOnStack = m_iFramesOnStack - _
intFramesToPop

' make top frame visible
m_objFrameStack(m_iFramesOnStack - 1).Visible = _
True

' refresh the form
objForm.Refresh
End Function

Public Sub StackEnum()
Dim strMsg As String
Dim i As Integer

' iterate through stack in reverse order
For i = m_iFramesOnStack - 1 To 0 Step -1
' build string of frame names
strMsg = strMsg & m_objFrameStack(i).Name & _
vbCrLf
Next i

' show stack
MsgBox strMsg, vbInformation, "Stack Contents"
End Sub

Pushing Frames

Let's look at the PushFrame method first. PushFrame takes two input parameters: the Form on which the frame resides and the Name of the frame itself.

The first task PushFrame does is to get a reference to the frame itself through the Controls collection of the Form

Set objThisFrame = objForm.Controls(strFrame)

Next we must handle the case when there are no frames yet added. Since eVB variables are Variants, we need to initialize our array counter.

If IsEmpty(m_iFramesOnStack) Then
m_iFramesOnStack = 0
End If

Next, the stack array is resized and the frame is pushed onto the top (last element) or the stack.

ReDim Preserve m_objFrameStack(m_iFramesOnStack)
Set m_objFrameStack(m_iFramesOnStack) = _
objThisFrame

Previous Page  Next Page