Close a windows form without exiting the entire application - c#

Environment
Windows XP SP3 x32
Visual Studio 2005 Standard
Device/Platform: Honeywell Dolphin 9500 with Windows Mobile/Pocket PC 2003
NET Framework 1.1 and .NET Compact Framework Framework 1.0 SP3
Goal
I currently have an application with 3 forms. The first form is something like a splash screen but I have not yet decided whether or not the user will be allowed to reopen it. The second form is an aggregate listing of items that will be displayed, one by one, in the third form.
I would like to be able to open the first form and wait for a button press. When that button is pressed, I would like to open another form and dispose of the first. Once an item is selected out of the list box on the second screen, I would like to display the third form and possibly dispose of the second form. The user also needs to be able to reopen the second form to select another item to be displayed on the third form. That being said, I probably don't want to dispose of the second form. However, memory is an issue on this device (64MB shared between storage and system memory) hence my desire to dispose of things when I can.
Problem
You can probably guess this by the title, but when I close/dispose of my first form, the entire application closes. Now that I have read up on the matter a little, I am aware that this has to do with this line: Application.Run(new Form1()); or whatever my form happens to be named.
Things I Have Tried
this.Dispose() - closes the entire application
this.Close() - closes the entire application
I also saw multiple people recommending one instantiate their form (Form f1 = new MyForm();), show it (.Show();), and then use Application.Run(); with no arguments. When I try this, I get "No overload for method 'Run' takes '0' arguments"
ApplicationContext does not exist in version 1.1 of the .NET Framework
Code
static void Main()
{
Application.Run(new Welcome());
}
private void btnBegin_Click(object sender, EventArgs e)
{
Form wof = new WorkOrderForm();
wof.Show();
wof.BringToFront();
// Here is where I would like to dispose of the Welcome form
}

You can call Application.Run() with your "main" form, this will still allow the application to close properly when the form closes, but hide it (Visible=false) whilst you show the splash screen, or just show the splash screen on top of it.

Create a hidden form that you pass to Application.Run(). When you decide it's time for the app to go down, close that hidden form.

I keep answering my own questions...
I posted this identical issue on the MSDN forums and was told to use the ApplicationContext object instead of a new Form object as a parameter in Application.Run. I am going to try that now. For now I will leave this unanswered.
EDIT: Well, I recant. Application context does not exist in the .NET Framework v1.1
EDIT2: Actually, it seems that it does (http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext(VS.71).aspx), however it does not appear to exist in the Compact Framework version 1.0 SP3.

Is it required to have 3 forms?
One way is to create 3 panels in 1 form and just show the active panel.

Related

How do I run other windows forms while enabling a reference? Once I connect my reference, the application gets stuck on only one form

visual studio problem
I am trying to run different windows forms but once I enable my reference to TrackerLibrary, my Application.Run gets stuck on only being able to run the last form it was on before enabling my reference. Is there some way to fix this? (new to coding so I don't know if I'm missing anything)
The Application.Run method is what starts your application and the program will not go past that method call unless the user exits the application.
Using Application.Run(Form mainForm) will only allow you to start your application with 1 open form and the application will exit when that form is closed.
If you'd like to open more than 1 Form on start, consider using Application.Run(ApplicationContext context).
Application.Run Method documentation
Your application doesn't get "stuck on one form". You can open as many other forms as you want. What you can't do is close that first form without the application exiting. When you call Application.Run and pass a form, that method will return if and only if that form closes, at which point your Main method will also complete, if you have no more code after the Application.Run call.
If you want to be able to close a form without the application exiting then you cannot pass that form to Application.Run. What you can do instead is derive your own class from ApplicationContext and then create an instance of that class and pass it to Application.Run instead. You can then put whatever logic you want in that class to open and close as many forms as you like.
I'm not going to provide any code here because I don't know specifically what it is you want to do. Better that you follow the link above and work through the example it provides in order to understand the concept, then implement it as you need. You might also like to check out my own example of using a custom ApplicationContext here.

c# Application exits when Main form closes

I'm writing a multi form application, and I'd like to be able to close Form1 with out the application closing.
Right now i'm using this.Hide to hide the form. Is there a way to set it so i can close any form, or the application should only exist when all the forms are closed?
I think i remember seeing something like that at one point, but that might not have been visual studio and c#.
In your Program.cs file you have a line like Application.Run(new Form1()). Replace it with
var main_form = new Form1();
main_form.Show();
Application.Run();
Also you need to close application explicitly by call Application.Exit() where you want.
One strategy is to do something like the following:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
}
If you place this on all of your forms, when the last one closes, the application will exit.
A multiform aplication should have a clear EXIT option (either by menu, toolbar), since you can not know that the user wants to close the program when the last window is closed (i supose that the user could go to the File/Open and open new windows)
An aplication that does something automatically that the user did not asked for can used frustration/confusion and spend time reopening the aplication.
Even the user can think that the application somehow crashed since he did not close it.
Based on your coments it's a single flow kind of application
I suggest that you implement the typical wizard interface on a single form with the BACK, NEXT, CANCEL buttons.
When the desired state has been reached, for example enough information has been gathered , the NEXT button is changed to a FINISH button.
Any time the user presses the CANCEL/FINISH button the window will close
And if you still want multiform you could still have it, in this you could have multiple flows at the same time and just finish or cancel the one that you want.

What is the difference between closing an installed application and stopping the debugging process in VS 2010

As the title states, I'm interested in differences between stopping the debugging process in the VS 2010 and closing that application after it is installed.
The reason I'm asking this is that I get a weird problem with my application - this application employs several parent-child form connections. The data transfer works fine, and this is the code I use to enable it:
In the child form:
public ParentForm Parent { get; set; }
And in parent form:
ChildForm CF = new ChildForm();
CF.ParentForm = this;
CF.ShowDialog();
In the parent form I mark the variables or methods that need to be shared as public, and the data transfer/modulation works fine.
The problem I'm getting is with a smart card reader. It should be called from one of the child forms (specifically third "generation" child form), and simply read the cards ID number. The problem is that this works perfectly the first time the child form is created, but stops working completely every next time (the child form has no special method defined in its FromClosing or FormClosed methods). I managed to "cheat" my way out, and defined the reader specific data in the parent (root "generation") form, and it works properly now, but I'm worried about what will happen when I install the application (I can't start with testing yet because the destination workstations are not available to me yet) - to be specific, will the reader stop working the first time the user closes the application. So my question is what does the VS do when it starts/finishes with debugging, and can these conditions be duplicated in the release version of the application.
Thank you

Can we and how to open a new window and closes the previous window in the same thread?

I am asking this, because i want to know if when we are running an app, for start if we have an window to authenticate like a Log In window, after validating the user, can we open the Main Window in the same Thread without creating a new one?
I am trying to do this in WPF, but i think that is same thing in WPF or in Windows Forms.
Yes, you can.
Just do it.
When you generate a Windows Forms application via the IDE, it will generate the code for one form, as well as a Main function that displays the form at runtime. You can rewrite the Main method so it displays one form modally then displays the next form.
But there's a simpler way to achieve your objectives:
Have two windows: your Main window, where most of the work is done, and the login screen.
In the OnLoad event of your main window, create an instance of your login window and call ShowModal() on this instance.
If the login fails, then exit the application.
This question does not offer enough context to tell you how to do this in your specific case. In general you can just Close() a window, construct a new one and call Show() on it.
You should make sure the Application.ShutdownMode does not kill off your application when the window is closed though.

Handle PocketPC's Task Manager Stop Running programs when using nested Forms

I have a PocketPC C# application written in Visual Studio 2005. It uses nested forms (the user is presented with a form with multiple buttons, when the user selects one a new form is opened).
I've added code so that the 1st form sets it's title to string.Empty to hide it from the Running Programs List. When the 2nd form is showing and the user uses the task manager to stop my app, the 2nd form gets the on close event.
Is there any way of knowing that the close event has come from the task manager so that I can close my application? At the moment when breakpointing the close event, I'm seeing the DialogResult being set as DialogResult.OK (Which isn't helpful) and the 2nd dialog is closed returning control to caller which thinks the user selected OK and opens the next dialog.
I've Googled for info but all the helpful code such as ClosingEventArgs aren't available in the compact framework. Any ideas?
I may be missing something but if your problem is distinguishing between the 2nd dialog being closed normally, and being closed using task manager, can you not set some kind of marker when the normal close action occurs, before closing? Logically then any close event where the marker has not been set will be down to the task manager?

Categories

Resources