Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

[ once4ever ]
  • Leaders
Posted

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. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted
A code example
:

[Code]
Dim MyWindow as TheFormIMade

Sub AddOrFocusWindow
If MyWindow Is Nothing
MyWindow = New TheFormIMade
Else
MyWindow.Focus
End If
End Sub
[/Code]

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted

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. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted

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.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

no no no!

 

use a factory pattern

hide the constructor.

 

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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

oh yeah. . . override the dispose method to set the shared instance to nothing:

 

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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...