Calling form knowledge of closing modeless form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

form.Show();
 
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..
 
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
 
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
 
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
 
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.newForm_Closing);
}

public Color Info{ get{return info;}}

private void newForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
theMainForm.modelessFormClosing(this);
}
}
 
Wow - excellent solution, that hadn't dawned on me!!!

Now that's what I call using the technology to your advantage!
 
Back
Top