I have a combo-box to set the user culture:
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?
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