473,396 Members | 2,076 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,396 software developers and data experts.

Keeping forms open after the main form is closed

There is probably an extremely simple answer to this question and I'm
just being foolish.

I have a main form (let's say FormMain) which opens other forms (let's
say Form1, Form2). How do I keep those created forms, Form1 and Form2,
from closing when I close FormMain? Right now what I'm doing is
threading them by wrapping the Show method and in that threaded wrapper
I have a while loop that just does Application.DoEvents() as long as
the form hasn't been closed.

private void ShowForm() // this is the threaded function
this.Show();
while(!closed)
{
Application.DoEvents();
}
}

and in the form's Closed event I set closed = true.

It works just fine, but I feel like it's probably inefficient and I
think there has to be a better (and probably simpler) way of doing it.
Any ideas? Feel free to mock me for my stupidity.

Nov 17 '05 #1
3 3760
You probably have your FormMain as your start up object. When you
close that form, it closes the app.

Try starting your app from a Sub Main and then call Application.Run
without any arguments. The caveat to this is that when you need to
exit the app, you need to call Application.ExitThread.

Nov 17 '05 #2
Yes I was reading about that on another forum. How am I supposed to
know when all of the forms have been closed and that I need to call
Application.ExitThread()?

Just spitballing here, but I could pass a reference to the main program
as a parameter when I create the forms, and then have them tell the
main program when they are closing. Main program keeps a counter on
the number of open forms and once it goes down to 0 calls
Application.ExitThread().

Does that make any sense or is there a better / simpler way of doing
it? As you can tell I'm trying to condition myself to program
efficiently... Help is much appreciated.

Nov 17 '05 #3
Hi stumorgan,
what Chris and yourself are saying is an efficient way to code, a simple
example of this could be:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication5
{
namespace WindowsApplication5
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Create main message pump
Application.Run(new Form1());
}
}
}

public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//Show other forms in seperate thread
Thread t = new Thread(new ThreadStart(ShowForms));
t.IsBackground = false;
t.Start();

//Close the main form - this will exit the main
//UI thread message pump, but we will have another one
this.Close();
}

/// <summary>
/// Keeps track of the number of open forms
/// </summary>
int _formCount = 0;

private void ShowForms()
{
Form f1 = new Form();
Form f2 = new Form();

//Add event handler to decrement count on close
f1.FormClosed += new FormClosedEventHandler(ChildFormClosed);
f2.FormClosed += new FormClosedEventHandler(ChildFormClosed);

//store how mant forms there are open
_formCount = 2;

//show both the forms
f1.Show();
f2.Show();

//Create a new message pump outside of the main
//UI thread message pump
Application.Run();
}

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
_formCount--;

//if no open forms - kill message pump
if (_formCount == 0)
{
//Tell the current thread to exit it message pump
Application.ExitThread();
}
}
}
}
Mark Dawson
http://www.markdawson.org

"st*******@gmail.com" wrote:
Yes I was reading about that on another forum. How am I supposed to
know when all of the forms have been closed and that I need to call
Application.ExitThread()?

Just spitballing here, but I could pass a reference to the main program
as a parameter when I create the forms, and then have them tell the
main program when they are closing. Main program keeps a counter on
the number of open forms and once it goes down to 0 calls
Application.ExitThread().

Does that make any sense or is there a better / simpler way of doing
it? As you can tell I'm trying to condition myself to program
efficiently... Help is much appreciated.

Nov 17 '05 #4

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

Similar topics

6
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were...
3
by: Glenn | last post by:
I am confused about when a form is unloaded, if ever. I have forms loading as MDI forms that have a withevents reference to a global class raising events. When the forms close, the closed event...
7
by: mabond | last post by:
Hi Can't get my head round something which I reckon should be simple. My app has a main form (frmMain). MdiContainer set to true. A menu item triggers a new form (frmSelect) whose parent is...
2
by: MORALBAROMETER | last post by:
Hallo, I want to write a programming with c#.Net having forms like MS Word. The main form A contain commonly used objects.I have many other forms (B,C,D) etc that have inherited from A. i realised...
3
by: Henry Jones | last post by:
I have a project that has 5 or 6 forms. VB.NET VS 2005 In the FormClosing Event of each form I have the following code: If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close() If...
13
by: Academic | last post by:
I have a MDI form, sometimes child forms and sometimes forms that are neither If I close the app the child forms closing and closed event happens followed by the Mdi form receiving the...
3
by: Arne Beruldsen | last post by:
The migration from VB6 is anything but easy. Ok...I have an introductory form (start-up is via sub main) and then several succeeding forms which gather some info. As soon as the info is gathered...
12
by: kronecker | last post by:
I found this nifty routine that closes a form one by one every time it is called. However, I need to hide them instead of closing them. Is there a way to alter the code? I assume it has something...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.