Closing modeless form causes parent form to disappear (or lose focus).

M

Marcus

Jazzyfoot ([email protected]) posted this message at 2003-02-11
15:40:00 PST:
"The problem occurs when my application creates multiple modeless
forms. What happens is that when these forms are closed focus changes
from the parent application to the last application which had focus
(e.g. Visual Studio .NET).

Here are the steps to reproduce the problem (using the application
below)...

1. Create a modeless form, set it's owner, and call Show() - (click
modeless).
2. Create a second modeless form, set it's owner, and call Show() -
(click modeless, the first form may require moving in order to be able
to click the button again).
3. Close the first modeless form using the X in the top right corner.
4. Close the second modeless form using the X in the top right corner.

After performing step 4 the parent form disappears and whatever
application had focus before my test application is activated.

Here is some code to illustrate the problem...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ModelessForm
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Modeless";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
ModelessForm newForm = new ModelessForm();

newForm.Owner = this;

newForm.Show();
}
}

public class ModelessForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public ModelessForm()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

private void InitializeComponent()
{
//
// ModelessForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(272, 198);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "ModelessForm";
this.ShowInTaskbar = false;
this.Text = "ModelessForm";

}
}
}"

He didn't get any replies. I have the same problem and want to know if
there is a solution.

Thanks, Marcus
 
A

Andrew \(Infragistics\)

Looks like a bug to me. It seems easy enough to workaround. Just catch the
Closed event of the form and activate the owner if the form being closed
contains the input focus.
e.g.
private void OwnedFormClosed(object sender, EventArgs e)
{
Form form = sender as Form;

if (form != null &&
form.ContainsFocus &&
form.Owner != null)
{
form.Owner.Activate();
}
}

Marcus said:
Jazzyfoot ([email protected]) posted this message at 2003-02-11
15:40:00 PST:
"The problem occurs when my application creates multiple modeless
forms. What happens is that when these forms are closed focus changes
from the parent application to the last application which had focus
(e.g. Visual Studio .NET).

Here are the steps to reproduce the problem (using the application
below)...

1. Create a modeless form, set it's owner, and call Show() - (click
modeless).
2. Create a second modeless form, set it's owner, and call Show() -
(click modeless, the first form may require moving in order to be able
to click the button again).
3. Close the first modeless form using the X in the top right corner.
4. Close the second modeless form using the X in the top right corner.

After performing step 4 the parent form disappears and whatever
application had focus before my test application is activated.

Here is some code to illustrate the problem...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ModelessForm
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Modeless";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
ModelessForm newForm = new ModelessForm();

newForm.Owner = this;

newForm.Show();
}
}

public class ModelessForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public ModelessForm()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

private void InitializeComponent()
{
//
// ModelessForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(272, 198);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "ModelessForm";
this.ShowInTaskbar = false;
this.Text = "ModelessForm";

}
}
}"

He didn't get any replies. I have the same problem and want to know if
there is a solution.

Thanks, Marcus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top