I have two windows forms.Now I have to close first one and show the second form and vice-versa.How can i do it.I was passing this pointer to the constructor of second form and then trying to
close it,but this did not work.I can not use showdialog here.
Add static variables to each form in the Program class:
static class Program
{
public static Form1 f1=null;
public static Form2 f2 = null;
public static FullClose=false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
f1=new Form1();
f2 = new Form2();
Application.Run(f1);
}
}
Then in the Form_Closing event of each form:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Program.FullClose==false)
{
e.Cancel = true;
this.Visible = false;
Program.f2.Show();
}
}
(Change Program.f2.Show() to Program.f1.Show() in Form2).
Of course this will stop the application from ever closing, so you should provide an extra button (or similar) that sets a boolean static variable (FullClose) that the Form_Closing events can check to see if they should properly close or not.
You must set in your application, the close is not then main form was closed, then if all forms all closed.
You must pass the pointer of the main form to second form and main form must have second form pointer.
Then implement in your forms OnClosing event, then in her implementation open other form if this success return, else set Cancel to true and return.
In every case there is one form, which represents the live of your application. This form will be started within your Program.cs file by calling
Application.Run(new MyForm());
If you try to start within this form another one and this will afterwards kill his creator, this will always lead to some bad designs.
Instead you should create some super-form. It is invisible and contains the glue code between those two forms. Right after startup it creates both forms and shows the first one. Also it registers to an (self-written) event in both forms which claims to show the other one. If your invisible form gets the event from one form it just makes the sender invisible and shows up the other one.
Last but not least you need in both (or at least in one) forms a second event, which will close the super form thous closing the application.
Related
I tried opening a second form using a button on my main form, but when I close the second window, I can't open it again.
I added the following code to my main form:
settings_window secondForm = new settings_window();
private void settings_button_Click(object sender, EventArgs e)
{
secondForm.Show();
}
But when I try to open the second form named settings_window the second time, I get the following error: System.ObjectDisposedException.
I found the following code to fix this but I don't know where to place it:
private void settings_window_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // Do not close the form.
}
You can avoid storing references of Forms and use a simple generic method that shows a Form when an instance of it already exists or creates a new one (and shows it) when none has been created before:
private void ShowForm<T>() where T : Form, new()
{
T? f = Application.OpenForms.OfType<T>().SingleOrDefault();
if (f is null) {
f = new T();
f.FormClosing += F_FormClosing;
}
f.Show();
BeginInvoke(new Action(()=> f.WindowState = FormWindowState.Normal));
void F_FormClosing(object? sender, FormClosingEventArgs e)
{
e.Cancel = true;
(sender as Form)?.Hide();
}
}
When you need it, call as ShowForm<SomeFormClass>(), e.g.,
ShowForm<settings_window>()
Note:
This code uses a local method to subscribe to the FormClosing event.
You can use a standard method, in case this syntax is not available.
BeginInvoke() is used to defer the FormWindowState.Normal assignment. This is used only in the case you minimize a Form, then right-click on its icon in the TaskBar and select Close Windows from the Menu. Without deferring this assignment, the minimized Form wouldn't show up again.
When the starting Form closes, all other Forms close as well
This code supposes nullable is enabled (e.g., see object? sender). If nullable is disabled or you're targeting .NET Framework, remove it (e.g., change in object sender)
Is secondForm a private field of the main form class?
It should work then.
Alternative solution is to show it as as modal - ShowDialog()
Also if you want to save some data in your second form, just use some data initialization from constructor, then saving to first/parent form.
I think you need to create a new instance of the form each time you want to open it. It will create a new instance of the settings_window form each time the button is clicked.
private void settings_button_Click(object sender, EventArgs e)
{
// Create a new instance of the form
settings_window secondForm = new settings_window();
secondForm.Show();
}
Your code shows a class that you have named settings_window, which gives us a hint about what its intended use might be. In general, for a form that behaves "like" a settings window, that you can call multiple times using the same instance, you can declare a member variable using this pattern:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Provide some means, like a menu or button, to show 'secondForm'
settingsMenu.Click += onClickSettingsMenu;
// Dispose the settings_form when the MainForm does.
Disposed += (sender, e) => secondForm.Dispose();
}
// By instantiating it here, its public default or persisted
// properties are immediately available, for example even
// while the main form constructs and loads the initial view.
settings_window secondForm = new settings_window();
private void onClickSettingsMenu(object? sender, EventArgs e)
{
if(DialogResult.OK.Equals(secondForm.ShowDialog()))
{
// Apply actions using the properties of secondForm
}
}
}
This is suitable for any form when you want to:
Repeatedly show and hide the form (e.g. a "Settings" form where the user can change the options multiple times).
Retrieve the default or the persisted properties of the form from the outset even if it's never been shown.
Use any of the form's public properties (e.g. GameLevel, SortType etc.) at any given moment while the app is running, even if the form isn't currently visible.
Display the form modally meaning that "no input (keyboard or mouse click) can occur except to objects on the modal form".
The reason it works is that calling ShowDialog (unlike calling Show) intentionally does not dispose the window handle, and this is to support of this very kind of scenario. The app is then responsible for disposing the resource when the app closes.
The Microsoft documentation for ShowDialog explains how this works.
Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.
I'd like to have a welcoming form, named the StartForm. This closes, then opens my MenuForm.
Current problem is: First one stays open, doesn't close to show the second one.
I tried several things, like simply showing and hiding them. I now try Application.run and then hide it. Unfortunately it stays open. And doesnt Application.run the second form.
static void Main()
{
Form StartForm = new Main();
MForm MenuForm = new MForm();
Application.Run(StartForm);
//
Task.Delay(500).ContinueWith((t) =>
{
StartForm.Hide();
Application.Run(MenuForm);
});
}
I expected this to Close the first form after waiting the delay, but it stays open.
Calling Application.Run() causes a new application message loop to begin running on the current thread. If a subsequent call to Application.Run() is made while the first application is running an InvalidOperationException is thrown according to the docs. However, this is not actually possible in your case as your call to Application.Run() is being invoked on the UI thread, which blocks the execution of any following code until it exits.
Instead, what we need is to bootstrap the application by using Application.Run(new Main()) and then inside of the Main form we will handle the displaying and hiding behaviour.
Here is how I would suggest achieving this:
Firstly, in your Main method, replace everything with Application.Run(new Main());.
Then inside of your Main form create a method like the following:
private void Main_Shown(Object sender, EventArgs e)
{
Task.WaitAll(Task.Delay(500));
this.Hide();
var menuForm = new MForm();
menuForm.Closed += (obj, args) => { this.Show(); };
menuForm.Show();
}
And lastly, go into your Main.Designer.cs file and make sure you subscribe the Shown event to Main_Shown method you just created in the InitializeComponent method like so:
this.Shown += new System.EventHandler(this.Main_Shown);
Explanation of what is going on here:
According to the docs the Form.Shown Event gets fired the first time a form is shown to the user. This is perfect for this use case, so we 'hook' into it and respond to it by awaiting a delay of 500 milliseconds.
Afterwards we hide the current (Main) form, create the new (MForm) form and show it. But the more important detail is that we subscribe to that form's Closed event so that when it is closed by the user, the Main form shows itself again.
use timer to Open Second form in Firstform
and Hide Firstform after show Secondform.
like this.
Main
static void Main()
{
Form StartForm = new Main();
Application.Run(StartForm);
}
FirstForm
set timer with interval 5000 and Enable it.
private void timer1_Tick(object sender, EventArgs e)
{
MForm MenuForm = new MForm();
this.Hide();
MenuForm.Show();
timer1.Stop();
}
I have a windows forms question:
Program.cs:
Application.Run(new frmStart());
frmStart: on btnLoad_Click,
frmLoad formLoad = new frmLoad();
formLoad.Show();
this.Hide(); // if I do a this.Close(); after it shuts down and doesn't get to show the form
frmLoad: on btnCancel_Click:
Application.Exit();
// or this.Close();
// or even: base.Close();
The form disappears but the program doesn't end, I still have to press the blue "Stop Debugging" to make it stop.
I have been looking... I know it is possible to make the program really stop, and not just freeze when you close the second form, even if you don't keep the first form on the screen, but can't remember and can't figure out how.
Ack, -1 on Application.ExitThread!
The issue is that you haven't closed the main form. The simplest way is to hook onto the 2nd form's Closed event and have it close the main form. For example the code to open the 2nd form changes to:
var newForm = new frmLoad();
newForm.FormClosed += (closedSender, closedE) => Close();
newForm.Show();
Hide();
This essentially sets up so that when the frmLoad form closes, the main form calls it's Close() method. I used a Lambda expression for the event handler, but you can just as easily create a private method accepting an (object sender, EventArgs e) and point .FormClosed at it.
*Edit: Sorry, missed that you only want to close on certain state. In which case on your frmLoad, create a public property such as:
public bool UserCancelled
{
get;
private set;
}
where the Cancel button sets this to True before closing the form. Your event handler in the main form changes to:
var newForm = new frmLoad();
newForm.FormClosed += (closedSender, closedE) =>
{
if (newForm.UserCancelled)
Close();
};
newForm.Show();
Hide();
In frmStart add:
public static frmStart Current;
Then in the constructor add:
Current = this;
Then in frmLoad: on btnCancel_Click:
frmStart.Current.Close();
You really should call Close() on both. That's the only clean way as otherwise the first form never is told to close down and doesn't clean up.
You may know that it's safe to do this, but someone else working on the code later may add code in the OnClose in the first form that they need called. They will say not nice things about you when they finally figure out why their code is not called.
If you close both, then your app will exit.
Please use the Application.ExitThread() method, the method exits the message loop on the current thread and closes all windows on the thread.
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exitthread
I vote yuck on all the answers. Override Form Closing in your primary form and close the secondary form first.
I am new to win forms (C#). The current form that I'm making has the option to create a new (blank) instance of itself, but if I ever close that first instance, all the others close as well. This is not what I want to happen. (Closing any form opened up from the first doesn't close any others, though)
I was thinking it may be because I am creating a new copy from within one of the copies/objects, so it is tied to that first object so it closes when it does; however, if I open up another form from the one opened from the first one and then close that one that was opened from the first one, the one that I opened up from it doesn't close.
I want it so that I can still close that first form without the others closing, and that when the last one closes, the program stops running.
Is there any way to do this?
You can run as many forms as you need but each in the separate thread
using System;
using System.Threading;
using System.Windows.Forms;
public partial class MyForm: Form
{
public MyForm()
{
InitializeComponent();
}
private void Button1Click(object sender, EventArgs e)
{
var t = new Thread(() => Application.Run(new MyForm()));
t.Start();
}
}
You should not have to close the Entry Point (MainForm) Form. Check the Main() method code in Program.cs code file.
public static void Main(string[] args)
{
// Starts the application.
Application.Run(new Form1());
}
Read this page.
To prevent a form from closing, handle the Closing event and set the
Cancel property of the CancelEventArgs passed to your event handler to
true.
You dont need to create new thread for every form. Just use this:
Form newForm1 = new Form();
this.Hide();
newForm1.ShowDialog();
this.Close();
Shortly:
The first form of every windows application is the main form.
If it closes, so does the application.
(See Program.cs for more details)
To solve this you can call Application.Run (While I don't know if it's smart or not)
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
new Form1().Show(); // any entry point with form
Application.Run(); // without main form
}
}
Program.cs
You can use like this code block.
But when any form closed, application not finished.
(Even if the last a form in application.)
Because application not included mainform.
If application has not any a opened form.
Application will suspend in process. You can see then the task manager.
As a result you will call manually Application.Exit() method in each form close events.
I have created 2 forms in VS Studio 2008 Express Edition and declare them with public static in main program.cs file
I just want to switch between the two forms with ShowDialog and Close but when trying to close the second form and open the first form again with showdialog it says I cannot use showDialog when the form is already visible, whereas it isn't true since I closed it before to show the second form.
It asked me to set the form visible property to false before using showdialog, so I did it
internal static void CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close();
SecondForm.Visible = false;
SecondForm.ShowDialog();
}
But then it says I cannot use ShowDialog because the form is already shown in Dialog Mode and that I must close it. So I did what it asked
internal static void CloseSecondForm(FirstForm FirstForm)
{
FirstForm .Close();
SecondForm.Visible = false;
SecondForm.Close();
SecondForm.ShowDialog();
}
But it still pretends that the form is already opened with ShowDialog !
Is this a Bug in my prog or in Winform ?
Update: this is the whole code I posted in 5th answer (I want to use showdialog and not show because I may have a 3rd form in Background that I don't want the user to access):
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 = new Form1();
Form2 = new Form2();
Form1.ShowDialog();
Application.Run();
}
// called from Form1 BUTTON
internal static void ShowForm2(Form1 Form1)
{
Form1.Hide();
Form2.ShowDialog();
}
// called from Form2 BUTTON
internal static void ShowForm1(Form2 Form2)
{
Form2.Hide();
Form1.ShowDialog();
}
I tried with Hide as suggested but it doesn't work either. This is the whole program, what I want to do is very simple: I have two forms initially created in program with one button on each form to close self and open the other. I put all the logic in program.cs below:
using System;
using System.Windows.Forms;
namespace twoforms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static Form1 Form1;
public static Form2 Form2;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 = new Form1();
Form2 = new Form2();
Form1.ShowDialog();
Application.Run();
}
// called from Form1 BUTTON
internal static void ShowForm2(Form1 Form1)
{
Form1.Hide();
Form2.ShowDialog();
}
// called from Form2 BUTTON
internal static void ShowForm1(Form2 Form2)
{
Form2.Hide();
Form1.ShowDialog();
}
}
}
This is a bug in your program. When you have two instances of a form (call them A and B), you obviously cannot continually show one from the other using ShowDialog. If you could do this, it would mean that A shows B modally, and B then shows A modally, and A then shows B modally etc. This would be like building a house with two bricks, where you just keep taking the bottom brick and placing it on top of the other.
Your best solution is to not make these forms static, and instead just create new instances of each form as you need them. Your second-best solution is to use Show instead of ShowDialog; if you only have one of these forms showing at a time anyway, ShowDialog has no purpose.
Static forms are almost always a bad idea (and I'm being polite about "almost"). If your forms are taking a long time to create, you should identify what resource is taking so long to load and cache that as a static object, instead of trying to cache the entire form as static.
This is from MSDN:
When a form is displayed as a modal
dialog box, clicking the Close button
(the button with an X at the
upper-right corner of the form) causes
the form to be hidden and the
DialogResult property to be set to
DialogResult.Cancel. Unlike modeless
forms, the Close method is not called
by the .NET Framework when the user
clicks the close form button of a
dialog box or sets the value of the
DialogResult property. Instead the
form is hidden and can be shown again
without creating a new instance of the
dialog box. Because a form displayed
as a dialog box is not closed, you
must call the Dispose method of the
form when the form is no longer needed
by your application.
So once you show a form using ShowDialog and you now want to close it, just let it return DialogResult.Cancel
This will hide (it will still be in memory) your first form. Now you can call ShowDialog on your second form. Again, if you want to switch to first form then let the second form return DialogResult.Cancel and now just call ShowDialog on first form.
Try to use Hide() instead of Close(). I had a similar problem in the past and Hide() worked for me.
I think you should really treat Modal dialogs like method calls, and try to use the result of the Call to ShowDialog to determine what you want to do next? if you have a requirement to switch between dialogs you should use some sort of result (Maybe simply DialogResult see my example) or a public property of the dialog to determine if you need to Show another dialog, If you need to call one modal dialog from another you should think of it like a stack of forms, that effectively (even if you do make one invisible before calling another) place one upon the other. You really want to minimise this stacking of forms.
This is a bit of a contrived example but each form here simply has a single button with DialogResult set to OK.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool exit = false;
while (true)
{
if (exit) break;
using (Form1 frm = new Form1())
{
switch(frm.ShowDialog())
{
case DialogResult.OK:
break;
default:
exit = true;
break;
}
}
if(exit) break;
using (Form2 frm = new Form2())
{
switch(frm.ShowDialog())
{
case DialogResult.OK:
break;
default:
exit = true;
break;
}
}
}
}
to exit simply click the red close (x) button.
Check out the difference between Close and Hide. And the difference between Show and ShowDialog.
It's not really clear to me what you want to achieve; you only (partially) describe what you done in code and the symptom of the problem you are having. Could you please describe what you are trying to do?
If your goal is to have two dialogs showing from your main window where only one of the two can be visible at the same time, then there are perhaps better solutions than using two static (=global) public Form instances that you show using ShowDialog.
Have you thought of using one dialog Form for this that just changes it appearance depending on the situation?