Public Class MyApp
Public Shared context As MyAppContext = New MyAppContext
Shared Sub Main()
context.AddForm(New MainForm)
Application.Run(context)
End Sub
End Class
Public Class MyAppContext
Inherits ApplicationContext
Private myForms As ArrayList
Public Sub New()
myForms = New ArrayList
End Sub
Public Sub AddForm(ByVal form As Form)
If myForms.Contains(form) Then
Return
End If
myForms.Add(form)
If myForms.Count = 1 Then
MyBase.MainForm = form
End If
AddHandler form.Activated, AddressOf Context_Activated
AddHandler form.Closed, AddressOf Context_Closed
End Sub
Private Sub Context_Activated(ByVal sender As Object, ByVal e As EventArgs)
MyBase.MainForm = CType(sender, Form)
End Sub
Private Sub Context_Closed(ByVal sender As Object, ByVal e As EventArgs)
myForms.Remove(sender)
If (CType(sender, Form) Is MyBase.MainForm) AndAlso (Me.myForms.Count > 0) Then
Me.MainForm = CType(myForms(0), Form)
End If
End Sub
End Class
Public Class MainForm
Inherits System.Windows.Forms.Form
Private button1 As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
InitializeComponent()
MyApp.context.AddForm(Me)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
Me.button1.Location = New System.Drawing.Point(88, 104)
Me.button1.Name = "button1"
Me.button1.TabIndex = 0
Me.button1.Text = "SubFormを表示"
AddHandler Me.button1.Click, AddressOf Me.button1_Click
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.button1)
Me.Name = "MainForm"
Me.Text = "MainForm"
Me.ResumeLayout(False)
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim newSubForm As Form = New SubForm
newSubForm.Show()
Me.Close()
End Sub
End Class
Public Class SubForm
Inherits System.Windows.Forms.Form
Private button1 As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Private Shared counter As Integer = 0
Public Sub New()
InitializeComponent()
MyApp.context.AddForm(Me)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
Me.button1.Location = New System.Drawing.Point(88, 104)
Me.button1.Name = "button1"
Me.button1.TabIndex = 0
Me.button1.Text = "MainFormを表示"
AddHandler Me.button1.Click, AddressOf Me.button1_Click
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.button1)
Me.Name = "SubForm"
Me.Text = "SubForm"
Me.ResumeLayout(False)
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim newMainForm As Form = New MainForm
newMainForm.Show()
Me.Close()
End Sub
End Class
|