vb.net singel instance mdi child func

i heard there is a way builtin in vb.net that alwas me to control how many instance i can have of a singel mdi child (like i can have only one mdi child of a parcticl form)
[172 byte] By [folen] at [2007-12-18]
# 1

You can create a singleton instance of any object...

Public Module Main

Private mdiCHildForm as Form2

Public Function GetmdiChild()as Form2

If IsNothing(mdiCHildForm) then

mdiCHildForm = New Form2

return mdiChildForm

else

return mdiChildForm

end if

end function

end Module

from the mainform you would have

GetmdiChild().Parent = Me

GetmdiChild().ShowDIalog

DMan1 at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

What you want is a combination of a Singleton/Listener pattern. . .

Work with me . . . (Download the source)

create a new Windows Application project,

call it MdiSingletonVB

change Form1.vb to MainForm.vb

in the designer, change IsMDIContainer to True

drop a menu strip on the form

put this code in the form


Public Class MainForm

Private Shared _instance As MainForm

Public Sub New()
MyBase.New()
InitializeComponent()
_instance = Me
End Sub

Public Shared ReadOnly Property Instance() As MainForm
Get
Return _instance
End Get
End Property

End Class



Add a new form, call it MdiSingletonChildBase. (All your Singleton MDI Child forms will inherit from this)

Put this code in it:


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms


Public Class MdiSingletonChildBase
Inherits Form

Private _lastState As FormWindowState = FormWindowState.Normal

Private Shared _instances As Dictionary(Of Type, MdiSingletonChildBase)

Shared Sub New()
_instances = New Dictionary(Of Type, MdiSingletonChildBase)
End Sub

Public Sub New()
MyBase.New()
InitializeComponent()
_instances(Me.GetType) = Me
Me.MdiParent = MainForm.Instance
Me.WindowState = FormWindowState.Normal
End Sub

Public Shared Function GetInstance(ByVal t As Type) As MdiSingletonChildBase
Dim result As MdiSingletonChildBase = Nothing
If Not _instances.TryGetValue(t, result) Then
result = CType(Activator.CreateInstance(t), MdiSingletonChildBase)
If (Not (result) Is Nothing) Then
_instances(t) = result
End If
End If
If (result Is Nothing) Then
Throw New ArgumentException("Invalid Type")
End If
Return result
End Function

Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
If (WindowState <> FormWindowState.Minimized) Then
_lastState = Me.WindowState
End If
MyBase.OnSizeChanged(e)
End Sub

Public Sub Restore()
Me.WindowState = _lastState
End Sub

Protected Overrides Sub OnActivated(ByVal e As EventArgs)
MyBase.OnActivated(e)
End Sub

Public Shadows Sub Show()
MyBase.Show()
Restore()
End Sub

Public Shadows Sub Show(ByVal owner As IWin32Window)
MyBase.Show(owner)
Restore()
End Sub

Protected Overrides Sub OnClosed(ByVal e As EventArgs)
_instances.Remove(Me.GetType)
MyBase.OnClosed(e)
End Sub
End Class


Add a new "Inherited Form", inherit from "MdiSingletonChildBase", call it Child1, change its text to "Child 1".

Add a new "Inherited Form", inherit from "MdiSingletonChildBase", call it Child2, CHange its text to "Child 2"

back in MainForm, edit the Menu Adding a menu looking like -

Open Form. . .
╚ Child1
╚ Child2

Put this code in MainForm


Private Sub Child1ToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Child1ToolStripMenuItem.Click
MdiSingletonChildBase.GetInstance(GetType(Child1)).Show()
MdiSingletonChildBase.GetInstance(GetType(Child1)).BringToFront()
End Sub

Private Sub Child2ToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Child2ToolStripMenuItem.Click
MdiSingletonChildBase.GetInstance(GetType(Child2)).Show()
MdiSingletonChildBase.GetInstance(GetType(Child2)).BringToFront()
End Sub


compile and run

BlairAllenStark at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...