How to close form and reopen it? - c#

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.

Related

Focus winform after application.run

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

How do I make a button make a form in another form?

So in my current situation I have my Desktop form set to open things inside that form through the MDI.Container.
I have a form automatically open when the Desktop form is started. After reading that form you would click "next" on the form button and it SHOULD open the new form in the same mdi container, but instead it opens it in a new window in my actual OS. I want it to open in the original MDI container in the Desktop form.
If you can help me out please do!
I've tried the following:
this.IsMdiContainer = true;
Welcome welcome = new Welcome();
welcome.MdiParent = this;
welcome.Show();
Would this help? I'm not sure to get your question
// C#
protected void MDIChildNew_Click(object sender, System.EventArgs e)
{
Form2 newMDIChild = new Form2();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
the problem is you make the form ismdicontainer = true inside code you should make form ismdicontainer true while designing, not in run time.

How to open a closed windows form in c#.NET?

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

C# Opening a new form and closing the other one

In my program I show a login form before the main form, once the program detects a successful login I use this:
MainForm Main = new MainForm();
Main.Show();
this.Hide();
This works fine but there is a problem, the login form is still open even though it is hidden so when the program is closed the process still hangs, how can I stop this from happening?
Sorry, forgot to add, using this.Close(); doesn't work and will completely close the program.
Try something more like this:
this.Hide();
Main.ShowDialog();
this.Close();
You want to hide the login form before you show the dialog, then close the login form after the dialog has been closed.
Simply closing the Login dialog will ultimately end the application, so that's not a real solution, but you still want to hide the login.
Simply put, put things in the order you want them to go in, especially when dealing with message loops.
First, you hide the login form.
Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed.
Last, once the dialog is closed, you close the login form, ending the application.
You need to specify your MainForm when you staring application and in the Load event handler of this form ask for login. In this case you will have runned application and Login for on the starting:
Program.cs
Application.Run(new MainForm());
MainForm.cs
private void Form1_Load(object sender, EventArgs e)
{
LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// check login here
}
}
P.S. Close will close your application completelly if it's main form of your application.
Change the main form to be MainForm, and when the application launches, in your MainForm_Load launch login form as a dialogbox, so they cannot access the main form.
If you need to be able to close the application from the login form, use Application.Exit(0);
If you don't want them to see the main form lookup and override SetVisibilityCore and call it inside MainForm_Load.
You can use ApplicationContext.MainForm to specify current main form for the application:
static class Program
{
public static ApplicationContext Context { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Context = new ApplicationContext(new LoginForm());
// pass Context instead of just new LoginForm()
Application.Run(Context);
}
}
then in in login handler:
private void login_Click(object sender, EventArgs e)
{
Program.Context.MainForm = new MainForm();
// close login form
this.Close();
// set up context to track main form instead of login
Program.Context.MainForm.Show();
}
How about this.Close() instead?

About form closing at runtime in C#

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.

Categories

Resources