I would like to reopen a closed windows form in c#.
I actually actively close the form to allow autoIT scripts to run on a different windows application. However I would like to reopen the same form after the scripts are done.
So...
this.Close();
.
.
.
Application.Run(new FormTestPage());
However this gives me an error that says to use Form.ShowDialogue instead.
I'm unsure how that works.
Thanks in advance!!
If you close the main form and you started the application with
var frm = new MainForm();
Application.Run(frm);
Then closing the main form will also close the application. Start the application like this instead
var frm = new MainForm();
frm.Show();
Application.Run();
Now when the application has to close, you must call Exit (because it is not closed automatically anymore):
Application.Exit();
You can do this in the main form in the FormClosed event when you are not just closing the form temporarily.
Now you can reopen the form normaly with:
var frm = new MainForm();
frm.Show();
var item = new FormTestPage();
item.ShowDialog();
Related
I have 2 winforms in an application in visual studio. The first one is kind of a loading screen, making sure there are no connection problems, then a second winforms opens, which would be a login form. I use this method to close the first form and open the second one:
this.Close();
th = new Thread(opennewform); // [opennewform thread: Application.Run(new Login());]
th.SetApartmentState(ApartmentState.STA);
th.Start();
and it works fine, but when the second form opens it loses focus. I've tried adding this.Activate, this.BringtoFront, this.Show to the second form, but it doesn't work. And what I need is after the first form closes and the second one opens, this second form is the "active" form, so it always pops up. I also needed to use the close/thread method so that the first form really closes and not just this.hide(); but actually stays in the background. Thanks in advance for the help.
You do not need to create a new thread. Instead, change the start code to something like this
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = new Form1();
form1.Show();
Application.Run();
}
I.e., remove the first form from Application.Run(), so that the application does not exit when this form closes.
Then open the second form with
Close();
var form2 = new Form2();
form2.Show();
Now, the application will not exit automatically when you close any form. Therefore, you must exit it explicitly with Application.Exit();, e.g. in the FormClosed event of the second form.
try:
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Close();
I am allowing user to change some settings (language - UI culture) and to show current form in new language, I need to close form and reopen it.
I am using Windows Forms. In Program.cs first I show Splash screen and then Login form and after this I run Main form with
Application.Run(new frmMain());.
I tried several solutions offered which includes:
Controls.Clear();
InitializeComponent();
also I tried
var form = new frmMain();
form.Show();
this.Hide();
and also
this.Close();// note I tried before and after.
var form = new frmMain();
form.Show();
I also tried in form closing event
Application.Run(new frmMain());
and one solution with opening new thread.
None of this worked, or it worked partially, had some issues like not closing program well, or some other.
Is there some new simple way to do this that works?
or I better show user dialog box message "Do you want to restart program to show new language?"
Thanks
To restart your application you can rely on Application.Restart() method. It shuts down the application and starts a new instance immediately:
Application.Restart();
You can create a new global form here:
public partial class Form1 : Form
{
frmMain form = new frmMain(); // add your form here
public Form1()
{
//something...
}
//some another functions ...
private void btnOpenYourForm_Click(object sender, EventArgs e)
{
form = new frmMain(); // open your form here
form.Show();
}
}
I hope it works for you.
I have a form from which I can open another form. I would like to prevent the user from switching to other forms when form 2 is opened. Thus, the user can only switch to other forms when this windows is closed.
Any help to do so?
Use ShowDialog method which open a new form in Modal state
Form2 frmNew = new Form2();
frmNew.ShowDialog()
You should use a (Modal) Dialog instead of a Window.
A dialog can only be exited by pressing a Cancel/OK button (or Escape key), while windows have normally a Close button.
Example to show a dialog:
// C#
//Display frmAbout as a modal dialog
Form frmAbout = new Form();
frmAbout.ShowDialog();
form.ShowDialog() instead of form.Show()
everyone.
How do I call a number of forms one after another (meaning call new form after previous one is closed).
I wrote something like this;
Form1.Show();
Form2.Show();
and it naturally results in opening both forms simultaneously. How to make function to wait for the fist form to be closed?
Open one form, handle the FormClosed event, and open the second form from within that. Note that this is only necessary because your forms are not modal. If you were using ShowDialog() the call would not return until the first form was closed and your code would work as it is currently structured.
If you require non-modal behavior (i.e., your user must be able to interact with the owner form while the owned form is open) then use something like this:
// very simplistic example...
Form1 frm = new Form1();
frm.FormClosed += delegate { new Form2().Show(); }
frm.Show();
I have two forms named frmRegistration & frmMain in my project in c#.
I have set frmRegistration as my start form.
User enters data in the frmRegistration form & presses submit button to get registered. Then, I want to close frmRegistration form & show frmMain form to the user.
I'm trying this by using Dispose() method of the frmRegistration. But, when I use this method, it disposes all my application execution because frmRegistration is the startup form.
I don't want this to happen. Can anyone solve this problem?
thanks.
Use Show() and Hide() methods.
private void btnSubmit_Click(object sender, EventArgs e)
{
...
var frm = new frmMain();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.Show();
this.Hide();
}
UPDATE:
If you don't want to have frmRegistration in memory, start your program in main form and add this in your MainForm's Shown event:
var frm = new frmRegistration();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.FormClosing += delegate { this.Show(); };
frm.Show();
this.Hide();
Now you can just close the registration form and automatically get back to main form.
Try setting frmMain as start up form and hiding it initialy, show frmRegistration, do what you have to do, and Dispose it.
You can also change you Program.cs main class with the Main() function to start frmRegistration and after positive DialogResult or another check it will then start with frmMain - as your main form and message loop.
At least there are two options:
1.Turn your Start up form into a singleton
When you need to hide it, call it's hide method
2.Have new different startup form, call it MainApp form or whatever, have it set to invisible, the you can do what ever you like with the other non-startup forms.