Blog Post created by GreenTech on Nov 30, 2017

I saw a recent request on Pi Square about opening and closing a processbook page, whilst creating a 'back' button to return to the previous.

This intrigued me and rather than answer the users question, I thought it might be an interesting exercise to extend the task to follow the trail as deep as you like (rather than 1 page back).

The following was my late-night attempt to make it work. I'm sure there are better ideas but this works and was fairly painless to implement.

It requires that each page has a public 'History' collection and use a common function 'Forward(page)' to navigate to another page. This function loads the page and inserts the last page name into the history collection (the stack). The back function pops off the stack and return.

I've played around with 3 pages built with the following code and it seems to work as a treat.


Within the 'ThisDisplay' object:

Public history As Collection


Within a module:

Sub Goto_Display1()
    Forward "Display1.pdi"
End Sub


Sub Goto_Display2()
    Forward "Display2.pdi"
End Sub


Function Path(fullpath As String) As String
    Dim parts() As String
    parts = Split(fullpath, "\")
    Path = Left(fullpath, Len(fullpath) - Len(parts(UBound(parts))))
End Function


Sub Forward(page As String)
    Dim newdisplay As Display
    Set newdisplay = Application.Displays.Open(Path(ThisDisplay.Path) & page, Null)
    If ThisDisplay.history Is Nothing Then Set ThisDisplay.history = New Collection
    ThisDisplay.history.Add ThisDisplay.Path
    Set newdisplay.history = ThisDisplay.history
    ThisDisplay.Close False
End Sub


Sub Back()
    Dim Last As String
    Dim newdisplay As Display
    If Not ThisDisplay.history Is Nothing Then
        If ThisDisplay.history.Count > 1 Then
            Last = ThisDisplay.history(ThisDisplay.history.Count)
            ThisDisplay.history.Remove ThisDisplay.history.Count
            Set newdisplay = Application.Displays.Open(Last, Null)
            Set newdisplay.history = ThisDisplay.history
            ThisDisplay.Close False
        End If
    End If
End Sub