多次调用FormClosing事件

Fáb*_*lva 3 c# infragistics winforms

我有一个组合框来设置用户文化: 用户文化组合框

如果我将Culture值更改x次,当用户尝试退出时,FormClosing方法将被触发x次.

这是我的FormClosing事件:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

这是我的组合框值更改事件:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

我必须清理并初始化控件以将UI更改为新文化,但这样做我是否在InitializeComponent()中多次分配FormClosing事件?我该如何避免这种行为?

kgz*_*dev 5

因为InitializeComponent,在那种方法forms design mode properties/events设定.所以每次增加FormClosing event一个.为避免这种情况,请在上面添加此行this.InitializeComponent();

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.FrmParent_FormClosing);
Run Code Online (Sandbox Code Playgroud)

注意:它仅解决FormClosing事件问题