1

请帮我解决按钮执行点击后关闭表单的问题,因为我只依赖窗口关闭按钮(右上角),我不使用额外的按钮来关闭表单。但是,该程序仍在运行,但在保存 .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;
        }
    }
4

2 回答 2

1

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

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

DialogResult = DialogResult.None;
于 2013-03-14T14:28:41.420 回答
0

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

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();
                }
于 2013-03-14T14:47:40.937 回答