can't close form

H

HW

Hi, i have a problem as follows

I have form1 which instantiates and displays form2

as Form2.Show

then when in Form2 i call Hide method Me.Hide

back in Form1 GotFocus event i have the following

If Not Form2 Is Nothing Then
Form2.Close()
EndIf

The Close method here does not clean up resources ie the Form2 ref is still
set to object, not nothing as it should be. Why can't i call the Close
method
at this point?
 
G

Ginny Caughey [MVP]

I'm not sure exactly what you're trying to accomplish here. For starters,
why not show Form2 using ShowDialog?
 
B

Brooke

Here is the code that I usually use to show secondary modal forms. This
approach will only show one id for your form if you bring up a task manager
or the Running Programs list.

This example is for an About form that is launched from a menu pick.

#region External Dll calls

[DllImport("Coredll.dll")] public static extern IntPtr FindWindow(
string lpClassName, string lpWindowName);

[DllImport("Coredll.dll")] public static extern bool
SetForegroundWindow(IntPtr hWnd);

#endregion


#region Menu "Help" - "About" event

private void mnuHelpAbout_Click(object sender, System.EventArgs e) {

frmAbout about = new frmAbout();



string temp = this.Text;

this.Text = "";

about.Text = temp;

bool sipState = sipMain.Enabled;

sipMain.Enabled = false;

about.ShowDialog();

about.Dispose();

this.Text = temp;



sipMain.Enabled = sipState;



IntPtr hwnd = FindWindow( null, this.Text);

if ( hwnd != IntPtr.Zero )

SetForegroundWindow( hwnd );

}

#endregion



You will have to place the "External DLL calls" region in your form class.
 
G

Ginny Caughey [MVP]

Brooke,

A couple of things - forms are not automatically disposed when they're
closed by design to improve performance in case you need them again. If the
CF gets low on resources, it will take action then.

And all you really need to do to hide a parent form from the Running
Programs list is to set its Text property to "" before you call ShowDialog
on the new form and then set it back when you return. And if you launch the
secondary form using ShowDialog, even if a user attempted to activate the
primary form from the Running Programs list (assuming that you didn't hide
it by setting the Text to "") then the secondary form would still be
activated and not the primary one.

--
Ginny Caughey
Device Application Development MVP


Brooke said:
Here is the code that I usually use to show secondary modal forms. This
approach will only show one id for your form if you bring up a task
manager or the Running Programs list.

This example is for an About form that is launched from a menu pick.

#region External Dll calls

[DllImport("Coredll.dll")] public static extern IntPtr FindWindow(
string lpClassName, string lpWindowName);

[DllImport("Coredll.dll")] public static extern bool
SetForegroundWindow(IntPtr hWnd);

#endregion


#region Menu "Help" - "About" event

private void mnuHelpAbout_Click(object sender, System.EventArgs e) {

frmAbout about = new frmAbout();



string temp = this.Text;

this.Text = "";

about.Text = temp;

bool sipState = sipMain.Enabled;

sipMain.Enabled = false;

about.ShowDialog();

about.Dispose();

this.Text = temp;



sipMain.Enabled = sipState;



IntPtr hwnd = FindWindow( null, this.Text);

if ( hwnd != IntPtr.Zero )

SetForegroundWindow( hwnd );

}

#endregion



You will have to place the "External DLL calls" region in your form class.



Ginny Caughey said:
I'm not sure exactly what you're trying to accomplish here. For starters,
why not show Form2 using ShowDialog?
 
B

Brooke

I dispose of the About form as it will rarely be viewed.

The reason to use SetForegroundWindow is to ensure that the main form will
be displayed after the secondary form is closed. If the secondary form is
active and the user switches to another application and then back to the
secondary form and closes it then the main form will not be the active
window, the other application will be the active window. The
SetForegroundWindow will make the main form active and display it. Or at
least this is what happened on CF1...

=== Posted with Qusnetsoft NewsReader 3.3

----- Original Message -----
From: "Ginny Caughey [MVP]" <[email protected]> Sent:
Thu, 13 Jul 2006 14:33:52 Subject: Re: can't close form

Brooke,

A couple of things - forms are not automatically disposed when they're
closed by design to improve performance in case you need them again. If the
CF gets low on resources, it will take action then.

And all you really need to do to hide a parent form from the Running
Programs list is to set its Text property to "" before you call ShowDialog
on the new form and then set it back when you return. And if you launch the
secondary form using ShowDialog, even if a user attempted to activate the
primary form from the Running Programs list (assuming that you didn't hide
it by setting the Text to "") then the secondary form would still be
activated and not the primary one.

--
Ginny Caughey
Device Application Development MVP


Brooke said:
Here is the code that I usually use to show secondary modal forms. This
approach will only show one id for your form if you bring up a task
manager or the Running Programs list.
This example is for an About form that is launched from a menu pick.
#region External Dll calls
[DllImport("Coredll.dll")] public static extern IntPtr FindWindow(
string lpClassName, string lpWindowName);
[DllImport("Coredll.dll")] public static extern bool
SetForegroundWindow(IntPtr hWnd);
#endregion


#region Menu "Help" - "About" event
private void mnuHelpAbout_Click(object sender, System.EventArgs e) {
frmAbout about = new frmAbout();


string temp = this.Text;
this.Text = "";
about.Text = temp;
bool sipState = sipMain.Enabled;
sipMain.Enabled = false;


this.Text = temp;


sipMain.Enabled = sipState;


IntPtr hwnd = FindWindow( null, this.Text);
if ( hwnd != IntPtr.Zero )
 
G

Ginny Caughey [MVP]

Brooke,

I don't see why that shouldn't continue to work fine with CF 2 also.

--
Ginny Caughey
Device Application Development MVP


Brooke said:
I dispose of the About form as it will rarely be viewed.

The reason to use SetForegroundWindow is to ensure that the main form will
be displayed after the secondary form is closed. If the secondary form is
active and the user switches to another application and then back to the
secondary form and closes it then the main form will not be the active
window, the other application will be the active window. The
SetForegroundWindow will make the main form active and display it. Or at
least this is what happened on CF1...

=== Posted with Qusnetsoft NewsReader 3.3

----- Original Message -----
From: "Ginny Caughey [MVP]" <[email protected]> Sent:
Thu, 13 Jul 2006 14:33:52 Subject: Re: can't close form

Brooke,

A couple of things - forms are not automatically disposed when they're
closed by design to improve performance in case you need them again. If
the
CF gets low on resources, it will take action then.

And all you really need to do to hide a parent form from the Running
Programs list is to set its Text property to "" before you call ShowDialog
on the new form and then set it back when you return. And if you launch
the
secondary form using ShowDialog, even if a user attempted to activate the
primary form from the Running Programs list (assuming that you didn't hide
it by setting the Text to "") then the secondary form would still be
activated and not the primary one.

--
Ginny Caughey
Device Application Development MVP


Brooke said:
Here is the code that I usually use to show secondary modal forms. This
approach will only show one id for your form if you bring up a task
manager or the Running Programs list.
This example is for an About form that is launched from a menu pick.
#region External Dll calls
[DllImport("Coredll.dll")] public static extern IntPtr FindWindow(
string lpClassName, string lpWindowName);
[DllImport("Coredll.dll")] public static extern bool
SetForegroundWindow(IntPtr hWnd);
#endregion


#region Menu "Help" - "About" event
private void mnuHelpAbout_Click(object sender, System.EventArgs e) {
frmAbout about = new frmAbout();


string temp = this.Text;
this.Text = "";
about.Text = temp;
bool sipState = sipMain.Enabled;
sipMain.Enabled = false;


this.Text = temp;


sipMain.Enabled = sipState;


IntPtr hwnd = FindWindow( null, this.Text);
if ( hwnd != IntPtr.Zero )
SetForegroundWindow( hwnd );




You will have to place the "External DLL calls" region in your form
class.
 
H

HW

I can't use ShowDialog because Form2 opens up Form3 which then returns
to Form1. Form2 is hidden at some point in between and closed when Form1
has focus.

It is strange my Close method is not working in my Form1 gotfocus event.
Hope this clarifies.
 
S

Simon Hart

I do a similar thing on CF 2.0 as what you do with p/invoking
SetForegroundWindow, and it works fine.

If the question you are asking is when you dispose of a form the reference
to the object is not null. This is the case, but if you tried to access the
form through that same disposed reference, you will get an
ObjectDisposedException. It's just the reference which is invalid which the
GC will clean up when its good and ready.

Regards
Simon.

Brooke said:
I dispose of the About form as it will rarely be viewed.

The reason to use SetForegroundWindow is to ensure that the main form will
be displayed after the secondary form is closed. If the secondary form is
active and the user switches to another application and then back to the
secondary form and closes it then the main form will not be the active
window, the other application will be the active window. The
SetForegroundWindow will make the main form active and display it. Or at
least this is what happened on CF1...

=== Posted with Qusnetsoft NewsReader 3.3

----- Original Message -----
From: "Ginny Caughey [MVP]" <[email protected]> Sent:
Thu, 13 Jul 2006 14:33:52 Subject: Re: can't close form

Brooke,

A couple of things - forms are not automatically disposed when they're
closed by design to improve performance in case you need them again. If
the
CF gets low on resources, it will take action then.

And all you really need to do to hide a parent form from the Running
Programs list is to set its Text property to "" before you call ShowDialog
on the new form and then set it back when you return. And if you launch
the
secondary form using ShowDialog, even if a user attempted to activate the
primary form from the Running Programs list (assuming that you didn't hide
it by setting the Text to "") then the secondary form would still be
activated and not the primary one.

--
Ginny Caughey
Device Application Development MVP


Brooke said:
Here is the code that I usually use to show secondary modal forms. This
approach will only show one id for your form if you bring up a task
manager or the Running Programs list.
This example is for an About form that is launched from a menu pick.
#region External Dll calls
[DllImport("Coredll.dll")] public static extern IntPtr FindWindow(
string lpClassName, string lpWindowName);
[DllImport("Coredll.dll")] public static extern bool
SetForegroundWindow(IntPtr hWnd);
#endregion


#region Menu "Help" - "About" event
private void mnuHelpAbout_Click(object sender, System.EventArgs e) {
frmAbout about = new frmAbout();


string temp = this.Text;
this.Text = "";
about.Text = temp;
bool sipState = sipMain.Enabled;
sipMain.Enabled = false;


this.Text = temp;


sipMain.Enabled = sipState;


IntPtr hwnd = FindWindow( null, this.Text);
if ( hwnd != IntPtr.Zero )
SetForegroundWindow( hwnd );




You will have to place the "External DLL calls" region in your form
class.
 
G

Ginny Caughey [MVP]

Yes that does clarify thanks. Have you considered using ShowDialog on all
the forms, and when Form3 closes and returns to Form2, then that form closes
itself, which would return you to Form1 again?
 
S

scorePro

This is exactley how we do things in our application. The main from is
created in sub main as a dialog. All other forms are then created as
dialogs on top of that. this means that as each form closes the
previous one gets the focus, until the main form closes. This will
cause the application to end. I have tried many different solutions to
this but this method always seems to be the best one, simple and
effective

Adrian Collins
Developer @ scorePro.co.uk
 
S

scorePro

Ginny,

Simple is THE ONLY way with the compact framework :blush:)

Adrian Collins
Developer @ scorePro.co.uk
 

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

Similar Threads


Top