簡體   English   中英

為什么FormClosed事件處理程序兩次被調用?

[英]Why Does the FormClosed Event Handler Get Called Twice?

StackOverflow可能不是一個為什么問題的正確地方,但是我正在尋找一個“原因”答案而不是“如何”答案。 我已經通過禁用處理程序中的處理程序來解決此問題。

該應用程序具有一個DataGridView,可在進貨檢查期間顯示庫存信息。 數據網格對於屏幕而言太寬,需要水平滾動。 為了使數據更易於查看和編輯,添加了模態編輯器。 有兩個按鈕可以關閉模​​式編輯器,即“保存”或“取消”。 使用模式編輯器表單右上角的關閉按鈕應執行與取消按鈕相同的操作。

單擊取消按鈕后,一切正常。 單擊關閉按鈕后,模式編輯器FormClosed事件將觸發兩次。 為什么模式編輯器FormClosed事件會觸發兩次? 我的代碼中有錯誤嗎?

    private bool CancelModalEditor()
    {
        bool cancelled = false;

        string cancelMsg = (_cancelClicked) ? "Canceling" : "Closing";
        cancelMsg += " the editor will delete the Record with Serial Number: " + SerialNumber + " from the Audit Session. Is this what you want to do?";

        DialogResult dlg = MessageBox.Show(cancelMsg, "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dlg == DialogResult.Yes)
        {
            SaveClicked = false;
        }
        else
        {
            cancelled = true;
        }

        return cancelled;
    }

    private void AEMEBtn_Cancel_Click(object sender, EventArgs e)
    {
        _cancelClicked = true;
        if (!CancelModalEditor())
        {
            Close();
        }
        else
        {
            _cancelClicked = false;
        }
    }

    private void AEModalEditor_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (!_cancelClicked && !SaveClicked)
        {
            if (CancelModalEditor())
            {
                e.Cancel = true;
            }
            else
            {
                _cancelClicked = true;      // Prevent Infite Loop
                Close();
            }
        }
    }

調用模式編輯器的文件。

    private void ModalEditorForm_Closed(object sender, FormClosedEventArgs e)
    {
        AEModalEditor modalEditor = (AEModalEditor)sender;
        int currentRow = modalEditor.RowID - 1;

        if (modalEditor.SaveClicked)
        {
            UpdateDataGridRowWithModalEditorValues(dgAssetDetails, currentRow, modalEditor.AssetControlsValues);
            updateAuditDetailsDataGridRow(currentRow, modalEditor.AuditControlsValues);
            UpdateAuditTextFields(modalEditor);
            SelectAllCellsInRow(currentRow);
        }
        else
        {
            DeleteRowFromAllDataGridViews(modalEditor.SerialNumber, currentRow);
            _previouslySelectedRow = -1;
        }

        // Save all records in either case so that session data isn't lost.
        save(false);

        _currentlySelectedDataGrid = DataGrids.None;
        _modalEditorOpen = false;
        txtSerialNumber.Focus();
    }

不要在結束事件中再次調用close。 表單已經關閉,不需要再次關閉。

private void AEModalEditor_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (!_cancelClicked && !SaveClicked)
    {
        if (CancelModalEditor())
        {
            e.Cancel = true;
        }
        else
        {
            _cancelClicked = true;
            // You called Close here again
            Close();
        }
    }
}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM