473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reactivation of a closed MDI child

In the parent of my MDI application I declare a child form

private frmChild = null;

which is to be activated on a menu click. In the click event I am using the
following code:

if (frmChild == null || !frmChild .IsHandleCreated)
{
frmChild = new frmChild ();
frmChild .MdiParent = this;
frmChild .Show ();
}
else
frmChild .Activate();

This does not have the desired effect however; I am trying to avoid having
to go through initialization each time the form is re-opened. I also need
to make sure that only one instance of this form is opened at a time.

Can anyone offer help?

Thanks,
Nate

Nov 15 '05 #1
2 5919
"zobie" <NO*************@holmansnv.com> wrote in message
news:5Oq7b.14731$QT5.9360@fed1read02...
In the parent of my MDI application I declare a child form <snip> This does not have the desired effect however; I am trying to avoid having
to go through initialization each time the form is re-opened. I also need
to make sure that only one instance of this form is opened at a time.


I ran into your problem yesterday and came up with half of the solution: I
never close MDI children - just hide and unhide them (similar to [1]). The
problem was trying to close the entire app - which I just found the solution
in [2].

[1] http://tinyurl.com/mt46
[2] http://tinyurl.com/mt4t

To summarize here in the three steps (this isn't polished code, but should
give you the idea);

=============================
1. Add a closing event for the mdi child forms that cancels the close
(unless it's closing because of the parent closing), and instead just hides
the mdi child like this (you might want to use the sender object instead of
"this" depending upon where you put this event handler).

private void EntryForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MainForm parentFrm = (MainForm)this.MdiParent;
if (parentFrm.ClosingEventFromParent == false)
{
this.Visible = false;
e.Cancel = true;
}
}
=============================
2. Then to "activate" the mdi child, call this method from your menu buttons
like this:
BringToFront (mdiForm1Instance);
(You should probably use singleton pattern in your mdi children to really
prevent multiple instances)

void BringToFront (Form form)
{
if (form == null)
{
throw new ArgumentNullException ("form", "Can't bring a null form to
the front");
}
else
{
if (!form.Visible)
{
form.Show();
}

//form.WindowState = FormWindowState.Maximized;
form.BringToFront();
}
}
=============================
3. The important step is the closing method for the parent form. You can't
simply override onClosing or use the closing event, for reasons outlined in
[1] above. Instead add this to parent form:

const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;

public bool ClosingEventFromParent = false;
protected override void WndProc(ref Message m)
{
if ( m.Msg == WM_SYSCOMMAND && m.WParam == (IntPtr)SC_CLOSE )
{
ClosingEventFromParent = true;
}
base.WndProc(ref m);
}

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com

Nov 15 '05 #2
Thanks! This is exactly what I was looking for.

Nate
"Michael Mayer" <mi**@mag37.com> wrote in message
news:#5**************@TK2MSFTNGP12.phx.gbl...
"zobie" <NO*************@holmansnv.com> wrote in message
news:5Oq7b.14731$QT5.9360@fed1read02...
In the parent of my MDI application I declare a child form <snip>
This does not have the desired effect however; I am trying to avoid having to go through initialization each time the form is re-opened. I also need to make sure that only one instance of this form is opened at a time.


I ran into your problem yesterday and came up with half of the solution: I
never close MDI children - just hide and unhide them (similar to [1]).

The problem was trying to close the entire app - which I just found the solution in [2].

[1] http://tinyurl.com/mt46
[2] http://tinyurl.com/mt4t

To summarize here in the three steps (this isn't polished code, but should
give you the idea);

=============================
1. Add a closing event for the mdi child forms that cancels the close
(unless it's closing because of the parent closing), and instead just hides the mdi child like this (you might want to use the sender object instead of "this" depending upon where you put this event handler).

private void EntryForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MainForm parentFrm = (MainForm)this.MdiParent;
if (parentFrm.ClosingEventFromParent == false)
{
this.Visible = false;
e.Cancel = true;
}
}
=============================
2. Then to "activate" the mdi child, call this method from your menu buttons like this:
BringToFront (mdiForm1Instance);
(You should probably use singleton pattern in your mdi children to really
prevent multiple instances)

void BringToFront (Form form)
{
if (form == null)
{
throw new ArgumentNullException ("form", "Can't bring a null form to the front");
}
else
{
if (!form.Visible)
{
form.Show();
}

//form.WindowState = FormWindowState.Maximized;
form.BringToFront();
}
}
=============================
3. The important step is the closing method for the parent form. You can't simply override onClosing or use the closing event, for reasons outlined in [1] above. Instead add this to parent form:

const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;

public bool ClosingEventFromParent = false;
protected override void WndProc(ref Message m)
{
if ( m.Msg == WM_SYSCOMMAND && m.WParam == (IntPtr)SC_CLOSE )
{
ClosingEventFromParent = true;
}
base.WndProc(ref m);
}

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marco Nicosia | last post by:
Hello gang, My coworker and I are writing a Python class for the other developers within our team. This class is supposed to encapsulate several things, including daemonizing, setting up...
2
by: sameer | last post by:
Hi, My application is using VS2003 , VB.Net with Sql server 2000. The application has an MDI environment with several MDI child forms in the main MDI form. At times , just at times due to dont...
0
by: sameer | last post by:
Hi, My application is using VS2003 , VB.Net with Sql server 2000. The application has an MDI environment with several MDI child forms in the main MDI form. I use Devexpress Grids ( a third party...
3
by: Kiki | last post by:
Hello, i wonder if anyone can help.. Is there a way of knowing from the parent window (var window;) that the child window (var newWindow) has been closed? i can't touch the child window's closed...
0
by: **Developer** | last post by:
Form.MdiChildActivate Event Occurs when a multiple document interface (MDI) child form is activated or closed within an MDI application. I need to know in the MdiChildActivate event handler...
2
by: Gary Brown | last post by:
Hi, I need to either determine that a child form has been closed or disable the close box without removing the entire control box. Is there an easy way to determine if a child form has been...
1
by: jleblanc | last post by:
I need to be able to reference the main window, from a child of a child window but the middle child is closed. Here is the scenario... Main window = a new window = b (child window of a) new...
6
by: SAL | last post by:
Hi, VS2005 post I'm opening a window using the following syntax: Protected Sub lbEstValue_Click(ByVal sender As Object, ByVal e As System.EventArgs)...
29
Frinavale
by: Frinavale | last post by:
I have 2 FireFox (version 2) browser windows opened. One is the child of the other. When the user is finished with the child window, a method in the parent window is called to refresh a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.