0

我有一个表格来询问一些数据。在离开输入字段(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);

但我总是走入死胡同。

4

2 回答 2

1

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 函数被调用,你就强制关闭操作。

于 2013-02-25T14:30:51.717 回答
1

这可以解决问题:

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");
    }
}
于 2013-02-25T14:48:06.873 回答