1
votes

Kindly help me to solve this form closing fire after button perform click, since I only depend on windows close button (top right corner), I do not use additional button to close the form. However this program is still working but not automatically close the form right after save the .ini file..

I want the form closed ...after button1.performclick(), but I don't know what to do..

I have following codes:

    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
If you want people to help you please put some more effort in to formatting your code. I can appreciate that English may very well be your second language but you difficult to understand and the question is not clear.User 12345678
I'm sorry friend, but it's not clear what you're trying to do at all. We see the code, and vaguely understand what you're saying about the form closing on some button click, but you're not clearly explaining exactly what you want.Mike Perrenoud

2 Answers

1
votes

I'm not 100% sure what you want, but I'm pretty sure the answer you're looking for is in the DialogResult property of the form.

If your problem is a button is closing the form when you click it, then set DialogResult.None for the DialogResult property of the form.

DialogResult = DialogResult.None;
0
votes

I didn't got you correctly but if you want to close the form if the result was yes you can do such this.

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

where Dispose cleans all resources used by the program and then close it, or Close close the form directly

or try doing this if you want such this:

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