473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Closing a Form

Greetings,

I have a "newbie" question related to forms. I have the following code
in which I display a second form when a button on the first form is
pressed (using the "Click" event).

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
}

What I have noticed, however, is that I am still in "run" mode when the
code finishes executing. I thought the reason might be due to the fact
that I never closed the first form, I just hid it.

However, when I add the line of code, "ActiveForm.Close();" after
"frmInstructions.Show();", I have noticed that the second dialog never
even displays. Is there something obvious I am doing wrong?

[Note: I have also placed the following code in the "Click" event of the
command button on the second form, but I receive an error on that line
of code: "frmMain.ActiveForm.Close();"].

Thanks in advance!


*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
5 3376
Sherwood,
If you look at the code for your main form, there should be a "static
Main( )" method. This method should contain code that is roughtly:
Application.Run( new Form1( ) );

This Application object's Run method basically makes the instance of the
Form1 form the main form of the application, when this instance dies,
exentially you application ends. Now, if you hide this form and open
another, then close the latter, the instance of your main form is still
alive, it's just hidden, you need to close it in order for your application
to terminate. The other thing that you tried was closing the Form1 instance
before starting the other one ... not going to work cause then you're
actually ending your application right there and then.
Cordell Lawrence
Teleios Systems Ltd.

"Sherwood Page" <sp***@afo.net> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Greetings,

I have a "newbie" question related to forms. I have the following code
in which I display a second form when a button on the first form is
pressed (using the "Click" event).

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
}

What I have noticed, however, is that I am still in "run" mode when the
code finishes executing. I thought the reason might be due to the fact
that I never closed the first form, I just hid it.

However, when I add the line of code, "ActiveForm.Close();" after
"frmInstructions.Show();", I have noticed that the second dialog never
even displays. Is there something obvious I am doing wrong?

[Note: I have also placed the following code in the "Click" event of the
command button on the second form, but I receive an error on that line
of code: "frmMain.ActiveForm.Close();"].

Thanks in advance!


*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
Hi Cordell,

Thanks for the reply. However, I have tried placing
"ActiveForm.Close();" after the code that displays the second form, but
when I do this the second form (i.e., "frmInstructions") never even
displays (and I am still left in "run" mode). The code I currently have
is below:

Code for "frmMain" (the 1st form):

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

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
ActiveForm.Close();
}

Code for "frmInstructions" (the 2nd form):

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
}

Thanks again!

Sherwood
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Sheerwood, Anytime you close the main for the application will die and your
second form will also die alone with it, thats why it doesn't show ... I'm
not sure what exactly your attempting to do, but this can be dealt with in
several was.
One of the simplest ways is to have the second form close the first (main)
form when it's done.
"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
Hi Cordell,

Thanks for the reply. However, I have tried placing
"ActiveForm.Close();" after the code that displays the second form, but
when I do this the second form (i.e., "frmInstructions") never even
displays (and I am still left in "run" mode). The code I currently have
is below:

Code for "frmMain" (the 1st form):

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

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
ActiveForm.Close();
}

Code for "frmInstructions" (the 2nd form):

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
}

Thanks again!

Sherwood
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Hi,

I have also tried that (i.e., attempting to close the first form from
within the second form), but I receive the following error:

"An unhandled exception of type 'System.NullReferenceException' occurred
in Test.exe. Additional information: Object reference not set to an
instance of an object."

Here is the code in the 2nd form:

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
frmMain.ActiveForm.Close(); // Is this syntax correct?
}

Is my syntax wrong for closing the first form (i.e., "frmMain")? (By
the way, the first form displays the second form when a button is
pressed. Once that 2nd form is displayed, the user then clicks the
"Close" button to dismiss the second form.)

Thanks in advance!

Sherwood

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Before creating the second for set it parent to the first.

So

private void btnClickMe_Click(object sender, System.EventArgs e)
{
this.Hide();
frmInstructions frmInstructions = new FrmInstructions();
frmInstructions.Parent = this; // Set the parent of the child for to the
main form
frmInstructions.Show();
}

Close code FrmInstructions

private void btnClickMe_Click(object sender, System.EventArgs e)
{
if(this.Parent != null)
this.Parent.Close(); // in this case this will close the
entire app since the parent is the main form
}

Hope this helps
Cordell Lawrence

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
Hi,

I have also tried that (i.e., attempting to close the first form from
within the second form), but I receive the following error:

"An unhandled exception of type 'System.NullReferenceException' occurred
in Test.exe. Additional information: Object reference not set to an
instance of an object."

Here is the code in the 2nd form:

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
frmMain.ActiveForm.Close(); // Is this syntax correct?
}

Is my syntax wrong for closing the first form (i.e., "frmMain")? (By
the way, the first form displays the second form when a button is
pressed. Once that 2nd form is displayed, the user then clicks the
"Close" button to dismiss the second form.)

Thanks in advance!

Sherwood

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
1
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
4
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.