Opening a new form without owner? - c#

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

Related

How to show a hidden form from a different form?

I have a Form1, Form2 and Form3, Form1 being the main form.
The program starts at Form1, when I click a "next" button Form2 is called and Form1 hides.
Inside Form1:
Form2 form2 = new Form2(this);
this.Hide();
form2.Show();
And inside Form2 there's an "exit" and a "next" button. When I click on exit button, Form2 is closed and goes back to Form1, unhiding it.
Inside Form2 and inside partial class Form2:
private Form parent;
public Form2(Form caller)
{
parent=caller;
}
On exit button:
this.Close();
parent.show();
And at the "next" button of Form2 I call Form3 and close Form2:
Form3 form3 = new Form3();
this.Close();
form3.Show();
Now that I'm in Form3 how can I unhide/show Form1 from Form3 ?
I know that I have to list here the solutions I've tried, but I'm just starting with OOP and I'm stuck trying to do this. I've tried several things but none of them make sense and don't know how to do this.
Just to clarify, I don't want to create a new instance of Form1 I just want to Show the one that's already created.
Any help is really appreciated.
Form1 should look like:
Form2 form2 = new Form2(this);
this.Hide();
form2.Show();
Form2 should look like:
Form3 form3 = new Form3(parent);
this.Close();
form3.Show();
Your form2 knows about your form1 via the parent member, and it can hence give your form1 to Form3 (which should also have a constructor that takes a Form1)
Every form should have a constructor that takes a Form1 and stores it in a parent variable. Every form other than Form1 should pass the parent variable to any new form it creates
You can make this easier using inheritance for the ctor, the next and the close functionality
I do not recommend calling your class Form- call it Form1 so it doesn't clash with System.Windows.Forms.Form
Actually you should call them all something better- MainForm, CreatePersonForm and CreateAddressForm for example

Top right x button doesn't completely close the program

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();
}
}

Hiding a child form close it instead and return me to the owner c#

I have a login form and after I validate the user I call the main form with the following code:
frmMain frm = new frmMain();
Hide()
frm.ShowDialog();
Show();
The problem is when I try to hide the main form with Hide() or Visible = false, to minimize the application to the tray bar, the main form gets closed and the application returns me to the login form like if I press the close button.
How does Hide() and Show() work? Does it create another instance?
When a modal form is hidden, it would return to the calling code. That's how it is. What i would do is change the code to either close the login form after showing the main form:
frmMain frm = new frmMain();
frm.Show();
Close()
If you want to show again the login form when the main form is closed, you could use the FormClosing event, like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
frmLogin frmlogin= new frmLogin();
frmlogin.Show();
}
Or call the main form in the beginning, and from it call the login form as a dialog.
Make two window form, 1:- Login (default open), 2:- Main (Open on login success)
1:- First login window will open
2:- after successfully login close/hide the login form and open/show the Main form which will also be window.
3:- handle the task bar display property in the same way.

Why hiding login form is closing Mainform unexxpectedly?

I have two forms in my application. When user opens up application, login Form appears where user enters login information and after he enters correct information, the login form should disappear and the Main Form must appear. The logic i am using to show Main form is:
if (this.userNameFld.Equals(this.userName) && this.pwdFld.Equals(this.pwd))
{
MainForm domain = new MainForm();
domain.Show();
this.Hide();
}
PROBLEM
The problem i am facing is that as the Login form disappears, suddenly the Main form appears and gets vanished at once. And for what reason its happening, i don't know. Please help me find one. Moreover, I don't have any FormClosing event in my Login form. So what could be the bug?
To resolve this you need to call the Main form from the main application thread instead of inside of the login form.
One option to do this is to run the application from a standard class (not a form) that calls the login form and then have some logic that creates the main form.
Another way is to create the Main form first but call the login form from the Main form before it is visible.
Form frmMain = new frmMain();
Then inside of frmMain , instantiate the login form
this.Hide();
StuRec_frm domain = new StuRec_frm();
domain.ShowDialog();
This solved my problem. Thanks everyone.
this.Hide();
is perhaps what is closing - remove this.Hide(); and try again
This code is just to demonstrate login and subsequent form usage(read comment to scott_f's response). Not really a response to actual question.
// Assuming correct login
// This will close the main form
//MainForm mainForm = new MainForm();
//mainForm.Show();
//Hide();
// After some more code
//Close();
// this will not close the main form as control execution will not move ahead until Main form is closed
MainForm mainForm = new MainForm();
Hide();
mainForm.ShowDialog();
Close();

Start new form on closing one. C#

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).

Categories