Close Form

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
C

Claudia Fong

Hello everybody,

I have a Menu form where I have a button.

The user should click the button and the program should open another
form call register form. I want that when the program show the register
form, the menu form will close or hiden.

I use the close () method, but it close everything (both forms) but if i
use hide() method.. even when I close all the forms using the X button
in the right top corner, it seems to me that the program is still
running in the background, because I need to manually click on the stop
button that is in C# express 2005

Is there a way to close just the menu form???

In VB6 all I needed to do is call registerform.show()
and menuform.close()

Can somebody help me?

Cheers!

Claudi
 
When the main form (startup form) is closed the application will terminate,
as you have found. You'll need to architect your application in such a way
to allow the main form to stay "alive" for the duration of your application.
 
Tim Wilson said:
When the main form (startup form) is closed the application will
terminate, as you have found. You'll need to architect your application in
such a way to allow the main form to stay "alive" for the duration of your
application.

This is the default behaviour, but it can be changed by using a class
derived from the ApplicationContext class. Here's an example of the
Program.cs file that does what the OP wants, i.e. Form1 is displayed when
the program starts, then when Form1 is closed Form2 is displayed, and when
Form2 is closed the program exits.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication1
{
class MyApplicationContext : ApplicationContext
{
public MyApplicationContext()
{
Form1 f1 = new Form1();
f1.FormClosing += new FormClosingEventHandler(form1_FormClosing);
f1.Show();
}
void form1_FormClosing(object sender, FormClosingEventArgs e)
{
Form f2 = new Form2();
// Next line is crucial. When ApplicationContext.MainForm is closed then the
program will exit.
MainForm = f2;
f2.Show();
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Note you run an instance of your ApplicationContext, NOT your main form.
Application.Run(new MyApplicationContext());
}
}
}

Chris Jobson
 
Back
Top