c# request FormClosing

本文关键字:FormClosing request | 更新日期: 2023-09-27 18:00:53

我有一个表单需要一些数据。在输入字段(TextBox,DGV(离开时,将调用相应的_Validating方法或_CellValueChanged方法。

如果我想结束程序,也要调用此方法-在调用_FormClosing方法之前

如何确定程序是否分支到_FormClosing方法?

private void txb_Validating(object sender, CancelEventArgs e)
{
    doLog("Text 1");
}
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    doLog("Text 2");
}
private void doLog(string txt)
{
    // this is first called at closing...
    if( [FormClosing is active] )
    {
        // Do something
    }
    else
    {
        // Do someting different
    }
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    // ... and this but later
    // Write the Logfile
}

如何替换[FormClosing is active]以获得正确的结果?

我试过了,

if ( this.FormClosing== true ) 

因此

this.FormClosing +=new FormClosingEventHandler(MyForm_FormClosing);

因此

FormClosingEventHandler cl = new FormClosingEventHandler(MyForm_FormClosing);

但我总是走在死胡同里。

c# request FormClosing

这样就可以了:

public class YourForm : Form
{
    private bool bIsClosing = false;
    public YourClass()
    {
       InitializeComponent();
       this.FormClosing +=
           new FormClosingEventHandler(MyForm_FormClosing);
    }
    private void txb_Validating(object sender, CancelEventArgs e)
    {
        doLog("Text 1");
    }
    private void dgv_CellValueChanged(object sender,
        DataGridViewCellEventArgs e)
    {
        doLog("Text 2");
    }
    private void doLog(string txt)
    {
        // this is first called at closing...
        if( bIsClosing )
        {
            // Do something
        }
        else
        {
            // Do someting different
        }
    }
    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        bIsClosing = true;
        // Write the Logfile
        doLog("whatever");
    }
}

this.FormClosing是一个在表单开始关闭(如单击关闭按钮(时触发的事件,因此得名。您需要您的应用程序来注册该事件,如下所示:

 this.FormClosing +=new FormClosingEventHandler(MyForm_FormClosing);

这样可以确保一旦触发FormClosing事件,就会调用您的MyForm_FormClosing

您可以创建一个类似bool bIsFormClosing的标志,并在调用关闭函数后设置该标志。

编辑:

我现在通过查看您的回答和评论了解到,您想在doLog函数中了解表单是否正在关闭。

这是的另一种方法

`

public class YourForm : Form
{
    private bool bIsClosing = false;
    Private bool bClosingHandled = false;
    public YourClass()
    {
       InitializeComponent();
       this.FormClosing +=
           new FormClosingEventHandler(MyForm_FormClosing);
    }
    private void txb_Validating(object sender, CancelEventArgs e)
    {
        doLog("Text 1");
    }
    private void dgv_CellValueChanged(object sender,
        DataGridViewCellEventArgs e)
    {
        doLog("Text 2");
    }
    private void doLog(string txt)
    {
        // this is first called at closing...
        if( bIsClosing )
        {
            // Do something
            bClosingHandled = true;
            this.close(); 
        }
        else
        {
            // Do someting different
        }
    }
    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        If(!bClosingHandled)
        {
          bIsClosing = true;
          e.Cancel = true;
          return;
        }
        // Write the Logfile
        doLog("whatever");
    }
}`

这种方法使用两个标志。。。当您第一次收到关闭事件时,您将bIsClosing标志设置为true,取消该事件并返回。然后,一旦调用了dolog函数,就强制执行关闭操作。