表格结束之后:button.performclick

时间:2013-03-14 14:22:51

标签: c#

请按钮执行点击后帮我解决此表格关闭火灾,因为我只依赖于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;
        }
    }

2 个答案:

答案 0 :(得分:1)

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

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

DialogResult = DialogResult.None;

答案 1 :(得分: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清除程序使用的所有资源然后关闭它,或者直接关闭表单

或尝试这样做,如果你想要这样:

  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();
                }