how to open form 1 on close button click X in form 2
i tried this but it is not working:
private void supplierShow_FormClosing(object sender, EventArgs e)
{
new suppliersList().Show();
}
Thank you.
Not fully sure without knowing more your code but to me it looks like you are declaring a local variable (new suppliersList of base class Form probably) and showing the form from the Closing event of another form.
Your Form2 object is probably going to be deleted/disposed/garbage collected soon and at that point I am not completely sure that the form declared within that scope would still have a nice life.
If Form2 is the application form, the application will actually terminate when you close it.
in general I think that this kind of form switching is best done and controlled from the main method, the same place where you probably have Application.Run(new Form2()); because in there you have full control of the application flow and MessageLoops...
I have to solutions:
First:
You may have a unique reference to your suppliersList in Program class:
public static suppliersList SuppList = new suppliersList();
you can now hide it if you want:
Program.SuppList.Hide();
you can show it anytime, too:
Program.SuppList.Show();
Be carefull! Don't Dispose SuppList. If you did, assign to it a new object new suppliersList();. Please note that this solution would success only if form1 is not the main form (i.e. Application.Run(new form1()); )
Second:
You can start form2 as a child of window of form1:
new suppliersList().Show(this);
because, as in the other answer, the application loops are on form1. If form1 is closed, then entire application will be closed too. However, starting a child window of the main window will prevent application close.
If the answer is useful for you, please mark it as your best answer.
Related
I have two forms in my c# windows project . The first form is called is called "loginform" and the other one is called "mainform" . I want to move to the mainform on button click But when i reached at the mainform , the problem is when i tries to close my second form which is named "mainform" here , the application does not close and i have to use the visual studio to terminate the project. below is the code for moving to second form
this.Hide();
mainform mf = new mainform();
mf.Show();
I found the answer. On the second form add the following code on the formclosed event .
private void mainform_FormClosed(object sender, FormClosedEventArgs e)
{
Application.ExitThread();
}
The problem you have is because “your code” basically “throws away” ANY reference back to this when the code sets it as “hidden”…
this.Hide();
After this line of code is executed, mainform is created and shown…
mainform mf = new mainform();
mf.Show();
Assuming, there is no code “after” the code above… then basically this has now been lost. The user can NOT see the form nor interact with it. mainform is shown… but it knows nothing about this. this is now lost and you have NO way of getting it back. That’s why it keeps running.
Obviously, mainform has no idea about this i.e. … the form that “created” it, however it DOES need this to stay alive. IF we “close” this and this created mainform… then mainform depends on this and will also close when this closes.
mainform mf = new mainform();
mf.Show();
this.Close();
Then, mainform gets killed as soon a this closes. So that will not work. However… if you changed the Show to ShowDialog… for the mf.ShowDialog(), then it would work and this would get closed properly.
Therefore, IF you want to “CLOSE” the form that “created” another form, then you should spin off another process for the mainform or pass this to the mainform and let it close it when it is done. I am sure there are other ways to do this and if Application.ExitThread(); ... works for you then go for it.
This question already has answers here:
How do I prevent the app from terminating when I close the startup form?
(4 answers)
Closed 7 years ago.
well, i did a multi forms app in c#. This means that i had more forms including one which is the main menu. All forms has ,among others,a button which return to the main menu.(Except from the main menu form which has buttons which access a specific form)
Well this is how i make this transition.
this.Close();
Form1 Myform1 = new Form1()
Myform1.Show();
The problem is that when i randomly close the app, it dissapear , as i want,but it doesn't close completely.I mean it only dissappeared, but it still runs.
WHY? and HOW TO DO IT TO CLOSE ENTIRELY ?
The basic problem you are running into is that by default, your Winforms program will exit when the main form (the one that is displayed first) is closed. This is because the reference to this form is passed to the Application.Run(Form) method in the program's Main() method.
There are a variety of ways to deal with the issue, but IMHO the most straightforward is to not close the form. Instead, call the Hide() method on your main form. This will cause it simply to not be shown, but not closed. When you want to return to it, just call Show() on that form instance and it will be visible again.
Naturally, to accomplish this you will need to pass that reference to any other code that may want to use it. So you may wind up for example with code that looks like this:
this.Hide(); // Not Close()!
Form1 Myform1 = new Form1(this)
Myform1.Show();
Then somewhere in the Form1 class, where you are ready to close that and show the main form again, you would call the Show() method. That might look something like this:
partial class Form1 : Form
{
private readonly MainForm _mainForm;
public Form1(MainForm mainForm)
{
_mainForm = mainForm;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
_mainForm.Show();
}
}
Another way to handle this is to just not have the MainForm passed to the Application.Run() method in the first place. Instead, call the parameterless overload of Application.Run(). But then you will need some other mechanism for closing the program. For example, calling Application.Exit() at the appropriate time.
While this seems to me to very a fairly common question, I was unable to find another question that seemed like an exact duplicate. However, there are definitely a large number of other related questions, including the following:
C# Application.Run without Form
Close a windows form without exiting the entire application
How can I close a login form and show the main form without my application closing?
If I close one of my forms, they all close
Methods this.hide() vs this.close()
Those last two are particularly relevant, but unfortunately the top answer in each is probably the worst way to solve the problem (i.e. put each form into its own thread…one should use multiple UI threads as a last resort only, and doing so is definitely not called for here). So take any advice you read in any of the above questions with a grain of salt. There is some good information among the answers, but there is also a fair amount of bad advice as well.
I am coding a simple sidescroller in C# using Windows Form Applications. I want it so when the player touches an exit point, this immediately loads a new level.
To implement this, I use the following code:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
Form2 myNewForm = new Form2();
myNewForm.Visible = true;
this.Hide();
}
This works. However, it loads numerous instances of form2 - I only want it to load once. I don't know how to write this (sorry, I'm a newbie - it took me a while just to write this code!).
Also, loading a level via a new form is inefficient. Is there a way to unload the open form to load the next one in the same window/instance, rather than creating another separate window?
Sorry if this is unclear. I've done my best research + I'm new. Please don't mention XNA! Thanks.
You need a small modification to your project's Program.cs file to change the way your app decides to terminate. You simple exit when there no more windows left. Make it look like this:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var start = new Form1();
start.FormClosed += WindowClosed;
start.Show();
Application.Run();
}
static void WindowClosed(object sender, FormClosedEventArgs e) {
if (Application.OpenForms.Count == 0) Application.Exit();
else Application.OpenForms[0].FormClosed += WindowClosed;
}
}
Now it is simple:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
new Form2().Show();
this.Close();
}
You can use Application.OpenForms[] collection to retrieve the Opened form instance and then Show it.
Try This:
Form2 frmMyForm = (Form2)Application.OpenForms["formName"];
frmMyForm.Show();
The actual problem is not in your form being loaded multiple times, but in your game logic not being suspended when the end of the level is reached. It means that your game keeps playing an old level when a new level is already loaded.
If Form1 is the main form then your whole application will shutdown. You need bootstraper which will be the entry point of your application, not the Form1. If you do that your Form1 will be a child in the same sense as will be Form2. You can open and close them without shutting down the application. If you don't know how to do that, just create another empty form, let's call it Main and make it the starting form of your application. Then hide it and open Form1 from Main form as Modal. Then when you complete level in Form1, close Form1, the code flow will return to Main form and you'll spawn Form2 from Main form as Modal. You'll have fully predictable logic, where all forms are opened from a single controlled place.
I'm making a small simple windows application. This is my main function:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// This will be my Form object
Form1 robotPath = new Form1();
Application.Run(robotPath);
// at this point I'll try to make changes to my object
// for instance I'll try to change a background image
robotPath.changeImage();
}
However after changing my object, the changes are not reflected in the output window (the background is not changed). I've tried robotPath.refresh() and robotPath.invalidate() but still the background is not changing. However, when I call the changeImage function using a button click event it works. But I want it to be changed without using a button/mouse event.(Background changes as the Form1 object is changed)
Any suggestion?
Application.Run()
does not return until the main form is closed. All code that runs after Application.Run() does not run until the program is shutting down. That's clearly not what you want.
You can solve the problem easily enough by reordering your main:
Form1 robotPath = new Form1();
robotPath.changeImage();
Application.Run(robotPath);
An alternative would be to move the call to changeImage into the constructor of Form1, or some event that fires early in the form's life, e.g. Load. This option better encapsulates the behaviour of the form.
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.