Amazingly (at least to me) there didn't seem to be an easy way to do this. I looked on the net and the solutions there seemed to use a lot of resources by basically keeping all forms that have ever been opened, and creating a new form as the navigation target without closing any old ones.
With people buying 4GB of RAM nowadays (probably we will be up to 100 GB as a standard configuration when you are reading this) it's probably no big deal. But my OC wants to close the no longer needed forms :)
Here's a way to do this - this is just 3 forms. Form1 can navigate to form2 by pushing the button, Form2 can navigate to Form3 by pushing the button, and Form3 closes the application when it's button is selected. I wanted to keep the code clear of distracting details, so I just used the default names etc. for the forms and buttons.
Option Strict On
Option Explicit On
Public Class Form1
Public continuation As FormContinuation
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' initialize the first continuation with Form2.
' later forms in the sequence are responsible for setting the continuation
' on this form to whatever should follow before they close themselves.
' We will just keep on opening and displaying those forms until
' someone sets it to nothing, after which the application will just
' close itself.
continuation = New Form2
Me.Hide()
While Not continuation Is Nothing
continuation.mainForm = Me
continuation.showFormDialog()
End While
' no more continuations, so just close this (the startup form)
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pt As New Point With {.X = 0, .Y = 0}
Dim sz As New Size With { _
.Width = Screen.PrimaryScreen.Bounds.Width, _
.Height = Screen.PrimaryScreen.Bounds.Height _
}
Me.Size = sz
Me.Location = pt
End Sub
End Class
Public Class Form2
Implements FormContinuation
Private m_mainForm As Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' we want the application to continue with Form3,
' so just set that in the mainform and then close this form
mainForm.continuation = New Form3
Me.Close()
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Property mainForm() As Form1 Implements FormContinuation.mainForm
Get
Return m_mainForm
End Get
Set(ByVal value As Form1)
m_mainForm = value
End Set
End Property
Public Sub showFormDialog() Implements FormContinuation.showFormDialog
Dim pt As New Point With {.X = 0, .Y = 0}
Dim sz As New Size With { _
.Width = Screen.PrimaryScreen.Bounds.Width, _
.Height = Screen.PrimaryScreen.Bounds.Height _
}
Me.Size = sz
Me.Location = pt
ShowDialog()
End Sub
End Class
Public Class Form3
Implements FormContinuation
Private m_mainForm As Form1
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Property mainForm() As Form1 Implements FormContinuation.mainForm
Get
Return m_mainForm
End Get
Set(ByVal value As Form1)
m_mainForm = value
End Set
End Property
Public Sub showFormDialog() Implements FormContinuation.showFormDialog
Dim pt As New Point With {.X = 0, .Y = 0}
Dim sz As New Size With { _
.Width = Screen.PrimaryScreen.Bounds.Width, _
.Height = Screen.PrimaryScreen.Bounds.Height _
}
Me.Size = sz
Me.Location = pt
ShowDialog()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' This is the last form in the sequence, so set the continuation to
' Nothing before closing
mainForm.continuation = Nothing
Me.Close()
End Sub
End Class
Option Strict On
Option Explicit On
Public Interface FormContinuation
''' <summary>
''' Get/set the mainForm of the series of forms
''' </summary>
Property mainForm() As Form1
''' <summary>
''' Show the dialog. This allows initialization to be
''' done before showing the dialog and to declare the
''' continuation as a FormContinuation yet still show the
''' dialog.
''' </summary>
Sub showFormDialog()
End Interface
No comments:
Post a Comment