2

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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
ExpertLoser
  • 257
  • 4
  • 14
  • 3
    You can use `Application.Restart();` – Reza Aghaei Jun 15 '18 at 21:26
  • Thanks Reza, what is amazing, I found this solution as you have posted it. It works. I can mark your answer as good. I have tested and it works, it restarts an app well, – ExpertLoser Jun 15 '18 at 21:30
  • Reza, you can add your answer as solution. – ExpertLoser Jun 15 '18 at 21:30
  • just to note: in FormClosing event you can put code to save changes to ApplicationSettings. There are 2 ways to do this and one way is much easier using myApplicationSettings.Save(); wrapper class MyUserSettings : ApplicationSettingsBase – ExpertLoser Jun 15 '18 at 21:34
  • As a side note, you can also change the culture at run-time. To do so, take a look at [this post](https://stackoverflow.com/a/46017814/3110834) or [this one](https://stackoverflow.com/a/21068497/3110834). You may find those posts useful. – Reza Aghaei Jun 15 '18 at 21:38
  • Reza, yes, I can change culture at runtime but still you need to refresh control captions in order to display it. Thats why I had to restart application, nothing else worked. – ExpertLoser Jun 15 '18 at 22:01
  • Take a look at linked posts. Both linked posts solve the problem. – Reza Aghaei Jun 15 '18 at 22:05
  • Yes, but I am using other third party components, not sure if it would work. – ExpertLoser Jun 15 '18 at 22:26
  • For standard windows forms controls it works properly. You can give it a try :) – Reza Aghaei Jun 15 '18 at 22:31

2 Answers2

2

To restart your application you can rely on Application.Restart() method. It shuts down the application and starts a new instance immediately:

Application.Restart();
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

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.