Bringing Form to Top/Front

  • Thread starter Thread starter Kevin Carter
  • Start date Start date
K

Kevin Carter

Hi there...hoping somebody can help me out because I am
almost ready to pull out my hair.

My problem is as follows:

In a method in my main form (non MDI app), I instantiate
and .Show another form. As a result of the .Show, the
form's Load event is called and I have a population
routine fires off. Normally, all is well and good and the
new form is active and gets displayed on top of the main
form.

I need to show a messagebox sometimes while populating the
new form, and whenever a messagebox is shown, the new form
always ends up "behind" the old form (but this only
happens when a messagebox is displayed).

After I .Show the form, I've all of the following tried
(not all at once):
newform.BringToFront();
....
newform.Activate();
....
oldform.SendToBack();
....

In addition...I've tried setting the messagebox to be
displayed on the Activated event. I've tried using the
MessageBox owner parameter and passing in the form.

Does anyone have any ideas? Thanks very much for your
help.

Kevin
 
Hi Kevin,
In addition...I've tried setting the messagebox to be
displayed on the Activated event. I've tried using the
MessageBox owner parameter and passing in the form.

This is the right direction to pursue. See the code sample below that
extends this idea and should help solving your problem:

protected override void OnActivated(System.EventArgs e)
{
base.OnActivated(e);

if (!_dataPopulated)
{
// THIS IS IMPORTANT!
this.Activate();

// Immediately prevent calling this code for the second time.
// This should be done before any actual work is done. Looks
// strange but works. You might need to re-name the variable to
// make the code more readable.
_dataPopulated = true;

PopulateData();
}
}
 
Back
Top