When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).
Related
So, my project currently has two forms (Form1, Form2). When a user presses the login button Form1 is hidden and Form2 is shown. For some reason while running the program, if you press the X at the top right of Form2 it will close Form2 but keep Form1 running and hidden. How do I set it so that the X button will close both forms completely (so that you don't need to go back into Visual Studio to close it).
I assume your Form1 is the 'main' form, in other words, that's the form that's launched when you launch the application. So this would be your main thread, and Form2 is a form that's opened upon some event, and that form runs in a different thread.
So, when the Form2 is closed if you want the main program to exit as well (although this doesn't quite seem like a great design idea), then you'd have to use Application.Exit().
You can capture your form 'closing' event in your Form2 by adding the Form2_FormClosing event handler, and in it, you call the above method.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
I'm trying to work with multiple forms, what I want is change the way the form is depending on the selected index of a combobox, the only way I could think of is hide form1 and show form2, but the problem is when you close form2, the process does not end...I tried the code below
private void Form2_FormClosing(object sender, FormClosedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("Process Name.exe"))
{
process.Kill();
}
}
if there isn't, is there a way the form can change on combobox selected index?
Try Application.Exit();
It exits your entire application and closes all your forms and threads.
Simply pass an instance of Form1 to the constructor of Form2, keep a reference to it in a form1 member
public class Form2 : Form{
private Form _form1;
public Form2(Form form1):this()
{
_form1 = form1;
InitializeComponent();
}
}
later you can simply use that reference :
_form1.Close();
This is a cleaner way to do it.
Other mechanisms are also ok, like implementing an eventhandler on form1 for an event in form2.
based on your pastebin code change this:
Form2 HeadquarterForm = new Form2(this);
you also only need the closed eventhandler and call close on the _form1 only once. So you don't really need the closing event handler.
The process is still running because form1 is still alive but hidden.
Try using Environmental.exit() to kill the process
Looking at your code in pastebin. Problem is, you're not passing Form1 in constructor of your Form2 when creating it. Change the part of your switch-case (4) to:
Form2 HeadquarterForm = new Form2(this);
I made a C# Windows Forms application that requires the user to login using a form called Form1 as shown below:
// This code is called from Form1
Form2 f = new Form2();
f.Show();
// Then Form1 does some finalization logic and closes itself
After the user logs in, the login form should close and the main application window Form2 should be opened. But the problem I'm running into is that if I call Form2 from Form1, Form1 becomes Form2's owner, thus closing Form1 closes both forms and ends the application.
How may I call Form2 such that it is independent of Form1?
The problem is not the owner, but that the Windows message loop is tied to Form1. When Form1 closes, so does the application. Look in your Main method:
Application.Run(new Form1());
The easiest solution is to show your login form (assuming it's a login form) as a modal dialog, and then begin the windows message loop on Form2:
static void Main()
{
var form1 = new Form1();
form1.ShowDialog();
if (form1.LoginSuccessful)
{
Application.Run(new Form2());
}
}
EDIT:
Just did some googling and looks like another alternative is custom ApplicationContexts. Never worked with these myself though:
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx
In C#, I'm trying to get two forms (but probably three eventually) to start at the same time... I've tried adding a new "Application.Run" to my Program.cs file, but it only starts the second form after the first one closes.
So how could I create something like that? Similar to a program like Lazarus.
You simply have to show your form before invoking Application.Run().
var form1 = new Form1();
var form2 = new Form2();
form1.Show();
form2.Show();
Application.Run();
Word of warning here, since no form is tied to the Application.Run call, you will need a way to tell the application to exit when all your forms are closed.
To display a Form you have 2 methods:
Show() - display a non modal dialog (is what you want); also you need to add Application.Run for to work.
ShowDialog() - display a modal(some blocking) dialog; a modal dialog capture all the input for the current thread.
If you want a interface like Lazarus, google by the "MDI application".
I think you probably should decide on one form to be your "primary" form, and then have the other form(s) be member variables of your main form. Then just have the Load event of your primary form also show the secondary form(s).
so,
class MainForm : Form {
readonly Form _otherForm = new OtherForm();
override OnLoad(EventArgs args) {
_otherForm.Closed += // add a handler for what happens when otherForm is closed.
_otherForm.Show();
base.OnLoad(args);
}
}
There may be a better way of doing this but that's what I would do as a first shot.
In my program I will press a button, and it will load form2. I do not want the program in form1 to continue running until the user closes form2. Then program execution continues in form1 right after the line that loads form2.
i think you want to show a modalled dialog.
public class MyForm1 : Form
{
public void ShowDialog2()
{
MyForm2 form2 = new MyForm2();
form2.ShowDialog(this);
}
}
You can use
Form.ShowDialog Method (IWin32Window)
You call the .ShowDialog function on the form you want to show.
.Show simply shows the new form. Calling .ShowDialog causes the calling form to block until execution returns from the shown form.
The result returned from the call to .ShowDialog will tell you if the user hit 'Okay' or 'No' or cancelled the form by clicking the x in the corner.