run window in a mdi app once!

hamid

Centurion
Joined
Jul 13, 2004
Messages
114
hi, i have a mdi application and menu "file>new win".when user click "new win" for first time i want create new window but when user click again and there is new window i want call same window.
please help me :D
 
Declare a boolean:
Public NewWindow As Form 'or whatever.

When you click on new win, in addition to creating a new instance of a form, you would set this to reference the NewWindow as well.

On the form, when you modify this new window so that it is no longer new, you should set this NewWindow variable to Nothing.

Then, when you click on new win, you would first check If NewWindow Is Nothing Then add the new window, otherwise, you would focus NewWindow. :)
 
A code example
Visual Basic:
:

[Code]
Dim MyWindow as TheFormIMade

Sub AddOrFocusWindow
    If MyWindow Is Nothing
        MyWindow = New TheFormIMade
    Else
        MyWindow.Focus
    End If
End Sub
 
And why would you have to have a code example explaining what I just said?

But, that was just the easy part. To get the MDIChildren to communicate with the MDIParent, you'd have to supply each MDIChild with a reference to the MDIParent so that you can set the NewWindow reference value. :)
 
thx but please give me VC#.net code:

MDIChild mychild=new MDIChild;
mychild.MDIParent()=this;
mychild.showform();
 
No, you need the parent's reference

private YourForm frm; //In the child form class.

In the constructor, you pass in the parentform as an argument and assign it to Yourform.
public void KidForm(YourForm F) { //Constructor.
frm = F;

Then, when you modify the form:

frm.NewWindow = null; // If you used the child's Parent property, you would not be able to access the .NewWindow property.
:)

Well, maybe you could cast it to a YourForm.
 
no no no!

use a factory pattern
hide the constructor.

Code:
Public Class SingleInstance
Inherits System.Windows.Forms.Form
 
'Static instance all references will use 
Private Shared theInstance As SingleInstance
 
'The constructor is marked as PRIVATE!!!
 
Private Sub New()
MyBase.New()
InitializeComponent()
End Sub
 
'Static Property returning the shared instance
 
Public Shared ReadOnly Property Instance() As SingleInstance
Get
If theInstance Is Nothing Then 
theInstance = New SingleInstance
End If
theInstance.Show()
theInstance.BringToFront()
Instance = theInstance
End Get
End Property

call it ala:
dim si as SingleInstance = SingleInstance.Instance
 
oh yeah. . . override the dispose method to set the shared instance to nothing:

Code:
Public Class SingleInstance
	Inherits System.Windows.Forms.Form
 
'Static instance all references will use 
	Private Shared theInstance As SingleInstance
 
'The constructor is marked as PRIVATE!!!
	Private Sub New()
		MyBase.New()
		InitializeComponent()
	End Sub

'Set the Shared Instance to nothing
 
	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)
		theInstance = Nothing
	End Sub
 
'Static Property returning the shared instance
 
	Public Shared ReadOnly Property Instance() As SingleInstance
		Get
			If theInstance Is Nothing Then
				theInstance = New SingleInstance
			End If
			theInstance.Show()
			theInstance.BringToFront()
			Instance = theInstance
		End Get
	End Property
End Class
 
Back
Top