3

I have a combo-box to set the user culture: User culture combo-box

If I change the Culture value x times, when the user tries to exit the FormClosing method will be fired x times.

This is my FormClosing event:

    private void FrmParent_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show(this, Properties.Resources.msgExit, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            e.Cancel = true;
    }

and this is my combo-box value changed event:

    void cbCulture_ToolValueChanged(object sender, ToolEventArgs e)
    {
        ComboBoxTool cbCulture = (ComboBoxTool)sender;
        var culture = cbCulture.Value.ToString();

        FormHelpers.SetCulture(culture);

        this.Controls.Clear();
        this.InitializeComponent();
        InitForm();
    }

I have to clean and initialize the controls to change the UI to the new culture but by doing so Am I assigning the FormClosing event multiple times in InitializeComponent()? How can I avoid this behavior?

Fábio Silva
  • 129
  • 2
  • 14
  • 2
    Its because of InitializeComponent, in that method events are added – kgzdev Feb 02 '17 at 12:14
  • sure, multiple subscription to the event. is it really necessary to clear the controls collection and call initializecomponent? it should be sufficient to assign culture specific properties (text, left-to-right, whatever) with the control layout intact, via assignments in `InitForm`? – Cee McSharpface Feb 02 '17 at 12:14
  • Not calling `InitializeComponent()` on each Culture change? – Pikoh Feb 02 '17 at 12:16
  • For culture work and changing all controls name it wouldn´t be necessary to call InitializeComponent(), why are you doing this?. – NicoRiff Feb 02 '17 at 12:17
  • Well, if I remove `Controls.Clear()` and `InitializeComponets()` the UI wont change to the new language. – Fábio Silva Feb 02 '17 at 12:20
  • 1
    see [Proper way to change language at runtime](http://stackoverflow.com/questions/11711426/proper-way-to-change-language-at-runtime) for more info – stuartd Feb 02 '17 at 12:30
  • `InitializeComponents()` is a part of winforms designer functionality. I wouldn't touch it. Mainly due to events (you subscribe **again** to all events every time), but potentially there could be other reasons. It's not meant to be called by you or reused. And it's located in constructor (constructor is called once) for a reason. The problem is actually bigger as you may have localized content already displayed, e.g. report is generated in English when you are switching to other language. Changing only UI controls language is not ok for the user. I'd show confirmation and reload complete form. – Sinatr Feb 02 '17 at 13:36

2 Answers2

5

Its because of InitializeComponent, in that method forms design mode properties/events setted. So everytime it adds FormClosing event one more. To avoid this add this line above this.InitializeComponent();

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.FrmParent_FormClosing);

Note: It solves only FormClosing event issue

kgzdev
  • 2,770
  • 2
  • 18
  • 35
  • yah -= solves this problem, but your answer made me realize that more problems may occur. There is any way to change the UI without cleaning and initializing controls? – Fábio Silva Feb 02 '17 at 12:26
  • 1
    You can remove any design time events from YourFile.designer.cs and set them manually in code. In that way InitializeComponent() method will not add every event many times – kgzdev Feb 02 '17 at 12:27
1

Remove the line that adds the FormClosing event handler from InitializeComponent:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmParent_FormClosing);

You can do this manunally or by deleting the handler using the designer.

Then, instead of adding this event handler using the designer, add it manually, e.g. by adding the same line to the constructor just after the call to InitializeComponent.

Nevertheless, calling InitializeComponent will also reinitialize your UI state. Maybe you might look for some other solution, such as this one (which I haven't personally tried and can't vouch for).

Joe
  • 122,218
  • 32
  • 205
  • 338