C
C
Chvalov2014-10-08 21:02:17
IDE
Chvalov, 2014-10-08 21:02:17

C# WinForm project does not exit completely after switching between forms, how to solve?

The essence of the question is clear from the title
There is a code (First form)

namespace coinBOT
{
    public partial class BotForm : Form
    {
        public BotForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SettingsForm открываем_форму_с_настройками = new SettingsForm();
            открываем_форму_с_настройками.Show();   // Показываем форму с настройками
            this.Hide();                            //Скрываем данную форму
        }
    }
}
Form 2
namespace coinBOT
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Hide();
                BotForm возврат_главной_формы = new BotForm();
                возврат_главной_формы.Show();
            }
        }
    }
}

If on the second form to remove Hide(); then you can see that the form is constantly working.
How can I kill the second form when closing the program from the first form to completely close the program?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dmmu, 2014-10-08
@Chvalov

I think something like this would be better for you:

namespace coinBOT
{
    public partial class BotForm : Form
    {
        private SettingsForm _settingsForm;

        public BotForm()
        {
            InitializeComponent();

            this._settingsForm = new SettingsForm() { Owner = this };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this._settingsForm.ShowDialog() == DialogResult.OK)
            {
                  // обработка результатов
            }
        }
    }
}

Remove the FormClosing handler from the second form, it is not needed there. The settings form will be killed automatically when the application closes.

S
Sumor, 2014-10-08
@Sumor

I feel you should read about ShowDialog .
If you open the form with ShowDialog, then the second form opens in modal mode, and the first one will wait for the return code from the second form.

C
cjey, 2014-10-08
@cjey

I draw your attention to the fact that when you close the second form, you do not show the already existing first form, but create another first form, and another first form remains hanging in the background.
Going back to the original question, you need to store all the forms you've created, and explicitly close them when you exit.
For example, you can modify Program.cs, create two static variables for the first and second forms. And already through these variables it is obvious to hide, show and close them.

I
Ivan Baidin, 2014-10-10
@zeronice

environment.exit(0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question