0

フォームX(右上)と閉じるボタンで同じ機能を作成するにはどうすればよいですか。これら2つは同じように動作する必要がありますこれは私がbtnClose_Clickに持っているものです

     private void btnClose_Click(object sender, EventArgs e)
            {
                DialogResult result;
                int fileId = StaticClass.FileGlobal;
                if (DataDirty)
                {
                    string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
                    MessageBoxButtons button = MessageBoxButtons.YesNo;
                    string caption = "Data Changed";
                    MessageBoxIcon icon = MessageBoxIcon.Question;
                    result = MessageBox.Show(messageBoxText, caption, button, icon);
                    if (result == DialogResult.No)
                    {
                        Program.fInput = new frmInputFiles(gtId, gName);
                        Program.fInput.Show();
                        this.Close();
                    }
                    if (result == DialogResult.Yes)
                    {
                        return;

                    }
                }
                else
                {
                    Program.fInput = new frmInputFiles(gPlantId, gPlantName);
                    Program.fInput.Show();
                    this.Close();

                }

            }

    Even on clicking the X to close the form,it should behave the same way as btnClose_Click

      private void frmData_FormClosing(object sender, FormClosingEventArgs e)
            {

        btnClose_Click(sender,e);//this doesnt seem to be working.
}

無限ループになります。私はそれがそれをしていることを理解しています..btnClose_Click()にはthis.Close()があり、frmData_FormClosing ..を呼び出し、次にbtnclose..を呼び出します。

ありがとう

4

1 に答える 1

6

this.Close()をbtnClose_Click()イベントに入れるだけです。次に、残りのすべてのロジック(一部を編集する必要があります)をfrmData_FormClosing()イベントに移動しe.Cancel = true;、フォームの閉じをキャンセルする場合(この場合、未保存の変更があり、ユーザーが[はい]をクリックした場合)を呼び出します。プロンプト。

次に例を示します(メモ帳にカットアンドペーストしただけなので、公正な警告です)。

private void btnClose_Click(object sender, EventArgs e)
{
    this.Close();
}

private void frmData_FormClosing(object sender, FormClosingEventArgs e)
{
    if (DataDirty)
    {
        if (MessageBox.Show("You have unsaved data. Do you want to save the changes and exit the form?",
                            "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        {
            Program.fInput = new frmInputFiles(gtId, gName);
            Program.fInput.Show();
        }
        else
            e.Cancel = true;
    }
    else
    {
        Program.fInput = new frmInputFiles(gPlantId, gPlantName);
        Program.fInput.Show();
    }
}
于 2012-06-27T02:59:25.470 に答える