【问题标题】:Form closing fire after: button.performclick表单关闭后触发:button.performclick
【发布时间】:2013-03-14 23:01:12
【问题描述】:

请帮助我解决此表单在按钮执行单击后关闭火灾的问题,因为我只依赖 Windows 关闭按钮(右上角),我不使用附加按钮来关闭表单。 然而,这个程序仍在工作,但在保存 .ini 文件后不会立即自动关闭表单..

我想关闭表单...在 button1.performclick() 之后,但我不知道该怎么做..

我有以下代码:

    int beFore, afTer;
    private void Form3_Load(object sender, EventArgs e)
    {
        beFore = this.checkedListBox1.CheckedIndices.Count +
                 this.checkedListBox2.CheckedIndices.Count +
                 this.checkedListBox3.CheckedIndices.Count +
                 this.checkedListBox4.CheckedIndices.Count;
    }
    //private Form4 subForm4 = new Form4();
    private void Form3_FormClosing(object sender, FormClosingEventArgs e)
    {
        afTer = this.checkedListBox1.CheckedIndices.Count +
                this.checkedListBox2.CheckedIndices.Count +
                this.checkedListBox3.CheckedIndices.Count +
                this.checkedListBox4.CheckedIndices.Count;
        while (beFore != afTer)
        {
            if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
                 MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                this.button1.PerformClick(); //need to close this form after button.performclick..
                this.UpdateForm1();
                break;
            }
            else
            {
                this.UpdateForm1();
                break;
            }
        }
        beFore = afTer;

    }

    private void UpdateForm1()
    {
        Form4 subForm4 = new Form4();
        subForm4.Show();
        subForm4.Update();
        try
        {
            int checkagain1 = this.checkedListBox1.CheckedIndices.Count; this.checkedListBox1.SetItemChecked(2, true);
            int checkagain2 = this.checkedListBox2.CheckedIndices.Count; this.checkedListBox2.SetItemChecked(2, true);
            int checkagain3 = this.checkedListBox3.CheckedIndices.Count; this.checkedListBox3.SetItemChecked(2, true);
            int checkagain4 = this.checkedListBox4.CheckedIndices.Count; this.checkedListBox4.SetItemChecked(2, true);

            Form1 myParentForm = (Form1)this.Owner;
            if (myParentForm.comboBox1.Text.Length != 0)
            {
                //myParentForm.Enabled = false;
                myParentForm.method1();
                myParentForm.method2();
                myParentForm.method3();
                myParentForm.method4();
                //myParentForm.Enabled = true;
                subForm4.Close();
            }

        }
        catch (Exception)
        {
            subForm4.Close();
            return;
            throw;
        }
    }

【问题讨论】:

  • 如果您希望人们帮助您,请在格式化您的代码方面付出更多努力。我可以理解英语很可能是您的第二语言,但您很难理解并且问题不清楚。
  • 对不起,朋友,但您根本不清楚您要做什么。我们看到了代码,模糊地理解了您所说的关于单击某些按钮时关闭表单的含义,但您并没有清楚地解释您想要什么。跨度>

标签: c#


【解决方案1】:

我不是 100% 确定您想要什么,但我很确定您正在寻找的答案在表单的 DialogResult 属性中。

如果您的问题是当您单击某个按钮时正在关闭表单,则为表单的 DialogResult 属性设置 DialogResult.None。

DialogResult = DialogResult.None;

【讨论】:

  • 非常感谢...但是使用 dialogresult.none 我正在使用 dialogresult.yes ...之后 this.button1.performclick()...
  • 这就是您在那里使用的弹出表单的 DialogResult。 Form4 也有一个 DialogResult。我是否正确地说您的问题是您的表单正在关闭而您不希望它关闭?
  • 不..我希望表单在保存文件后立即关闭。但是这两行代码正在工作..this.button1.PerformClick(); this.DialogResult = DialogResult.Yes;
【解决方案2】:

我没有正确理解您,但如果您想在结果为“是”的情况下关闭表单,您可以这样做。

if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
                 MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                this.Dispose();
                this.Close();
            }

其中Dispose会清理程序使用的所有资源然后关闭,或者Close直接关闭窗体

如果你想这样做,也可以尝试这样做:

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

    if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
                     MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    button1.PerformClick();
                }

【讨论】:

  • 对不起,如果你不明白......因为这是我第一次在这里发帖......但是“judgeja”的答案......正在进行一些修改,如下所示Dialogresult = DialogResult。是的;
猜你喜欢
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 2022-01-11
相关资源
最近更新 更多