表格与DialogResult结束

时间:2011-12-21 17:17:43

标签: c# winforms

我有Form1,上面有一个按钮。单击按钮后,Form2将会打开。

弹出的新表单要求用户输入数据以保存到文件中,单击Form2的按钮时,会将输入的用户数据保存到指定的路径。

以下是Form2的按钮点击代码:

private void dataBaseSaveButton_Click(object sender, EventArgs e)
    {
        if (firstNameTextBox.Text.Equals(string.Empty) || lastNameTextBox.Text.Equals(string.Empty) || payTextBox.Text.Equals(string.Empty))
        {
            MessageBox.Show("Please fill out all of the fields.", "Fields Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        else
        {
            //This is where I am saving the data to a file using StreamWrriter.

            this.Close();
        }

现在,Form2关闭了一个新的Form2将会打开,等等。(Form1中有一个循环可以执行此操作。)

我的问题是......

Form2我还有一个_FormClosed事件,如下所示:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        DialogResult userAnswer = MessageBox.Show ("Do you wish to close ALL " + counterLabel.Text + " of the 'Form2' forms?", "Close ALL Forms?", 
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (userAnswer == DialogResult.Yes)
            this.Dispose();
    }

所以我的问题是......

如何调用按钮点击以正确关闭Form2 WITHOUT 调用_FormClosed事件,同时保持表单的功能相同?

编辑:

我知道this.Hide();可以解决问题..但我想知道是否有办法不隐藏所有Form2,因为可能会有很多......

5 个答案:

答案 0 :(得分:3)

我认为您的应用程序有一种奇怪的行为。通常当用户输入数据行时,它在某种网格中执行。 但要回答您的问题,您应该删除事件处理:

private void Button_Click(object sender, EventArgs e)
{
    this.FormClosed -= Form_Closed;
    this.Close();
}

希望这能回答你的问题。

答案 1 :(得分:1)

你无法避免调用FormClosed,但你可以添加一些逻辑来忽略它。

e.g。当您强行关闭表单(类似bool _forceClose)时设置一个标志,并在FormClosed事件中检查该标志。如果已设置,则只返回并忽略其余的语句。

答案 2 :(得分:1)

认为您需要更改结束事件以确定是否需要取消它:

根据您的评论,您必须将局部变量设置为标志。 CloseReason中的FormClosedEventArgs属性将使用关闭表单的任一方法设置为UserClosing(尽管文档中提到了这一点)。

private bool _ConfirmClosing = true;

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
  if (_ConfirmClosing) {
    DialogResult userAnswer = MessageBox.Show ("Do you wish to close ALL " + counterLabel.Text + " of the 'Form2' forms?", "Close ALL Forms?", 
        MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (userAnswer == DialogResult.No)
      e.Cancel = true;
  }
}

答案 3 :(得分:1)

如果您可以关闭表单,只需引入一个新的公共表单属性来保存布尔值:

public boolean CanClose { get; set; }

然后在您的构造函数中将此值设置为false

public Form2()
{
   //....
   CanClose = false;
}

保存数据后,只需将此属性设置为true

private void dataBaseSaveButton_Click(object sender, EventArgs e)
    {
        if (firstNameTextBox.Text.Equals(string.Empty) || lastNameTextBox.Text.Equals(string.Empty) || payTextBox.Text.Equals(string.Empty))
        {
            MessageBox.Show("Please fill out all of the fields.", "Fields Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        else
        {
            //This is where I am saving the data to a file using StreamWrriter.

            CanClose = true;

            this.Close();
        }

最后,在结束活动中,只需检查您是否可以静默关闭表单,或者需要显示对话:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
      if (!CanClose) 
      {
         DialogResult userAnswer = MessageBox.Show ("Do you wish to close ALL " + counterLabel.Text + " of the 'Form2' forms?", "Close ALL Forms?", 
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
         if (userAnswer != DialogResult.Yes) 
         {
           e.Cancel = true;
           return;
         }
      }

       this.Dispose();
    }

答案 4 :(得分:0)

当以编程方式调用操作时,触发事件时,Windows.Forms库通常不一致。

我建议您在表单中保留一名成员,以了解您是否要忽略该事件。

private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
    if (firstNameTextBox.Text.Equals(string.Empty) || lastNameTextBox.Text.Equals(string.Empty) || payTextBox.Text.Equals(string.Empty))
    {
        MessageBox.Show("Please fill out all of the fields.", "Fields Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

    else
    {
        //This is where I am saving the data to a file using StreamWrriter.
        this.IgnoreCloseEvent = true;

        this.Close();

        this.IgnoreCloseEvent = false;
    }

然后

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    if (this.IgnoreCloseEvent)
         return

    DialogResult userAnswer = MessageBox.Show ("Do you wish to close ALL " + counterLabel.Text + " of the 'Form2' forms?", "Close ALL Forms?", 
        MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (userAnswer == DialogResult.Yes)
        this.Dispose();
}

必须一直这样做很痛苦,但对我来说,它总是比试图找出Windows.Forms库正在做的更好。

相关问题