By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

Calling form knowledge of closing modeless form

P: n/a

Does anyone know how to detect a modeless form on closing by the form that
invoked the modeless form?

form.Show();
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
RCS
I don't believe that there is an inherent way. How I'd do this, is maybe
have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...

Does anyone know how to detect a modeless form on closing by the form that
invoked the modeless form?

form.Show();

Nov 16 '05 #2

P: n/a
Thanks for the input.

It seems strange to me that there is no 'event' that could be triggered by
the modeless form to signal the parent/calling form it is closing.

I assume the child modeless form is a thread off the parent. There are ways
of monitoring a thread correct?

Gary

"RCS" wrote:
I don't believe that there is an inherent way. How I'd do this, is maybe
have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...

Does anyone know how to detect a modeless form on closing by the form that
invoked the modeless form?

form.Show();


Nov 16 '05 #3

P: n/a
The forms will run on the same thread, unless special action
is taken to run them on separate threads (but that is unusual).

I'm not sure I understood the original question clearly, but to have
the main form detect when a modeless form closes, it could simply
subscribe to the "Closed" event of the modeless form.

In the following very simple application clicking the main form will
open a modeless form, and when the modeless form is closed the
main form will respond by changing its background to some random
color.

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
}

protected override void OnClick(EventArgs e)
{
Form modelessForm = new Form();
modelessForm.Text = "Modeless Form";
modelessForm.Closed += new EventHandler(modelessForm_Closed);
modelessForm.Show();
}

static void Main()
{
Application.Run(new MainForm());
}

private void modelessForm_Closed(object sender, EventArgs e)
{
Random random = new Random();
BackColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
}

- Magnus
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Thanks for the input.

It seems strange to me that there is no 'event' that could be triggered by
the modeless form to signal the parent/calling form it is closing.

I assume the child modeless form is a thread off the parent. There are
ways
of monitoring a thread correct?

Gary

"RCS" wrote:
I don't believe that there is an inherent way. How I'd do this, is maybe
have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to
the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of
code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
>
> Does anyone know how to detect a modeless form on closing by the form
> that
> invoked the modeless form?
>
> form.Show();


Nov 16 '05 #4

P: n/a
Magnus,

Yes that is exactly what I wanted. Thanks for taking the time to reply.

I looked through MS and I did read about events and delegates but I was not
convinced that was what I needed.

I also read the great 'How to' on making a modeless form act as a modal form.

I have no idea what MS thinks when it post such complexity.

Now I am confused about the necessity of a 'delegate'.

Gary

"Magnus Krisell" wrote:
The forms will run on the same thread, unless special action
is taken to run them on separate threads (but that is unusual).

I'm not sure I understood the original question clearly, but to have
the main form detect when a modeless form closes, it could simply
subscribe to the "Closed" event of the modeless form.

In the following very simple application clicking the main form will
open a modeless form, and when the modeless form is closed the
main form will respond by changing its background to some random
color.

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
}

protected override void OnClick(EventArgs e)
{
Form modelessForm = new Form();
modelessForm.Text = "Modeless Form";
modelessForm.Closed += new EventHandler(modelessForm_Closed);
modelessForm.Show();
}

static void Main()
{
Application.Run(new MainForm());
}

private void modelessForm_Closed(object sender, EventArgs e)
{
Random random = new Random();
BackColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
}

- Magnus
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Thanks for the input.

It seems strange to me that there is no 'event' that could be triggered by
the modeless form to signal the parent/calling form it is closing.

I assume the child modeless form is a thread off the parent. There are
ways
of monitoring a thread correct?

Gary

"RCS" wrote:
I don't believe that there is an inherent way. How I'd do this, is maybe
have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to
the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of
code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
>
> Does anyone know how to detect a modeless form on closing by the form
> that
> invoked the modeless form?
>
> form.Show();


Nov 16 '05 #5

P: n/a
Or if you wanted the new form to pass information back to the main form, you
could provide a method in the main form to react to the new form ... The
2nd form could hide itself, then call the main form's method. The main form
could take the info it needs from the 2nd form and then call Dispose() on
the 2nd form.

The following should have the same behavior as Magnus's example (but in real
life, the newForm would be
responsible for collection the information so the MainForm could use it.)

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
}

protected override void OnClick(EventArgs e)
{
newForm modelessForm = new newForm(this);
modelessForm.Text = "Modeless Form";
modelessForm.Show();
}

static void Main()
{
Application.Run(new MainForm());
}

internal void modelessFormClosing(newForm frm)
{
BackColor = frm.Info;
}
}

public class newForm : Form
{
private Color info;
private Random random = new Random();
private MainForm theMainForm;

public newForm(MainForm frm)
{
theMainForm = frm;
info = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
this.Closing += new
System.ComponentModel.CancelEventHandler(this.newF orm_Closing);
}

public Color Info{ get{return info;}}

private void newForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
theMainForm.modelessFormClosing(this);
}
}

"Magnus Krisell" <ma******@NOSPAMstudent.liu.se> wrote in message
news:Of*************@TK2MSFTNGP15.phx.gbl...
The forms will run on the same thread, unless special action
is taken to run them on separate threads (but that is unusual).

I'm not sure I understood the original question clearly, but to have
the main form detect when a modeless form closes, it could simply
subscribe to the "Closed" event of the modeless form.

In the following very simple application clicking the main form will
open a modeless form, and when the modeless form is closed the
main form will respond by changing its background to some random
color.

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
}

protected override void OnClick(EventArgs e)
{
Form modelessForm = new Form();
modelessForm.Text = "Modeless Form";
modelessForm.Closed += new EventHandler(modelessForm_Closed);
modelessForm.Show();
}

static void Main()
{
Application.Run(new MainForm());
}

private void modelessForm_Closed(object sender, EventArgs e)
{
Random random = new Random();
BackColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
}

- Magnus
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Thanks for the input.

It seems strange to me that there is no 'event' that could be triggered by the modeless form to signal the parent/calling form it is closing.

I assume the child modeless form is a thread off the parent. There are
ways
of monitoring a thread correct?

Gary

"RCS" wrote:
I don't believe that there is an inherent way. How I'd do this, is maybe have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of
code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
>
> Does anyone know how to detect a modeless form on closing by the form
> that
> invoked the modeless form?
>
> form.Show();


Nov 16 '05 #6

P: n/a
RCS
Wow - excellent solution, that hadn't dawned on me!!!

Now that's what I call using the technology to your advantage!
"Magnus Krisell" <ma******@NOSPAMstudent.liu.se> wrote in message
news:Of*************@TK2MSFTNGP15.phx.gbl...
The forms will run on the same thread, unless special action
is taken to run them on separate threads (but that is unusual).

I'm not sure I understood the original question clearly, but to have
the main form detect when a modeless form closes, it could simply
subscribe to the "Closed" event of the modeless form.

In the following very simple application clicking the main form will
open a modeless form, and when the modeless form is closed the
main form will respond by changing its background to some random
color.

public class MainForm : Form
{
public MainForm()
{
Text = "Main Form";
}

protected override void OnClick(EventArgs e)
{
Form modelessForm = new Form();
modelessForm.Text = "Modeless Form";
modelessForm.Closed += new EventHandler(modelessForm_Closed);
modelessForm.Show();
}

static void Main()
{
Application.Run(new MainForm());
}

private void modelessForm_Closed(object sender, EventArgs e)
{
Random random = new Random();
BackColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
}

- Magnus
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Thanks for the input.

It seems strange to me that there is no 'event' that could be triggered
by
the modeless form to signal the parent/calling form it is closing.

I assume the child modeless form is a thread off the parent. There are
ways
of monitoring a thread correct?

Gary

"RCS" wrote:
I don't believe that there is an inherent way. How I'd do this, is maybe
have a public static collection property (that is of type Form) on the
"main" form class, and have the child windows add thier window handle to
the
collection when it starts, and remove it when it is closing.

meanwhile, on the main form, put your appropriate get and set chunks of
code
to handle other things.. for example, on your set, maybe you say:

if ( value.Text == "My Second Window" )
// do something

hope that helps..
"Gary Miller" <Ga********@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
>
> Does anyone know how to detect a modeless form on closing by the form
> that
> invoked the modeless form?
>
> form.Show();


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.