'VB.Net Example - Using an Owned Form
'
'(c) Jon Vote 07/2002
'Idioma Software Inc.
'All rights reserved
'
'www.idioma-software.com
'jon@idioma-software.com
'
'Code is presented as is.
'Use at your own risk.
'
'1) Start a new project. Form1 is created by default.
'2) From the menu select Project|Add Windows Form.
'3) Click the Open button.
'4) Form2 should now be added to the project.
'5) Display Form1 by double-clicking the Form1
' icon in the Solution Explorer (normally
' located to the right.)
'6) Press F7 to bring up the code window.
'7) Replace all of the code for Form1 with
' the following:
'
'VB.Net Example - Owned Forms
'
'(c) Jon Vote 07/2002
'Idioma Software Inc.
'All rights reserved
'
'www.idioma-software.com
'jon@idioma-software.com
'
'Code is presented as is.
'Use at your own risk.
'
'An Owned Form will always stay on top
'of the owner. If the owner is minimzed
'the owned form will also minimize.
'This example toggles Form2
'to be owned or not owned by Form1
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents m_frmForm2 As New Form2()
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnOwnForm As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnOwnForm = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnOwnForm
'
Me.btnOwnForm.Location = New System.Drawing.Point(8, 242)
Me.btnOwnForm.Name = "btnOwnForm"
Me.btnOwnForm.TabIndex = 0
Me.btnOwnForm.Text = "Own Form"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(294, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnOwnForm})
Me.Name = "Form1"
Me.Text = "www.idioma-software.com"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Display Form2 on startup.
m_frmForm2.Show()
End Sub
Private Sub m_frmForm2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_frmForm2.Closed
'Set our instance to Nothing if Form2 closes
m_frmForm2 = Nothing
End Sub
Private Sub btnOwnForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOwnForm.Click
'Show Form2 if not loaded.
If m_frmForm2 Is Nothing Then
m_frmForm2 = New Form2()
m_frmForm2.Show()
End If
'Toggle between Own or Dis-Own
If sender.text = "Own Form" Then
sender.text = "Not Own Form"
Me.AddOwnedForm(m_frmForm2)
m_frmForm2.Activate()
Else
sender.text = "Own Form"
Me.RemoveOwnedForm(m_frmForm2)
Me.BringToFront()
End If
End Sub
End Class