The NewForm did not close itself when every time i called a "New Form", but the Main Form does

Hi everyone, i have a problem. I already can closed the MainForm when i called the FirstForm, but when i called a "NewForm" again in the FirstForm, it called the SecondForm, but the FirstForm did not closed, so i have a 2 forms on my taskbar.

How do i fix that?

Here is the code:

private void AddNewForm(object sender, EventArgs e)
        {
            Form newForm = new Form();

            AddObjects(sender, e, newForm);

            UpdateTextPosition(sender, e);

            newForm.Size = new Size(1360, 735);

            newForm.Text = "Selling System";

            newForm.FormBorderStyle = FormBorderStyle.Fixed3D;

            newForm.AutoScaleMode = AutoScaleMode.Font;

            newForm.AutoScroll = true;

            newForm.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            newForm.StartPosition = FormStartPosition.Manual;

            newForm.Location = new Point(0, 0);

            newForm.MaximizeBox = false;

            newForm.Controls.Add(label1);

            newForm.Controls.Add(label2);

            newForm.Controls.Add(label3);

            newForm.Controls.Add(label4);

            newForm.Controls.Add(label5);

            newForm.Controls.Add(label6);

            newForm.Controls.Add(label7);

            newForm.Controls.Add(menuStrip1);

            CloseForm(sender, e);

            newForm.ShowDialog();
        }

        private void CloseForm(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                Action act = () =>
                {
                    this.Hide();
                };

                this.Invoke(act);
            }

            else
            {
                this.Hide();
            }
        }

Thanks in advance!

Dani AI

Generated

Short answer: the behavior comes from a mix of Hide/Close semantics and how you show the new window. Hiding a form does not dispose it, and calling ShowDialog() on a brand-new top-level Form (without an owner) gives it its own taskbar button. is correct that Close() disposes the form; ’s cast idea fails in many handlers because sender is usually the control that raised the event (not the Form).

Recommended patterns

  • To replace the current form (keep app running): show the new form first, then close the old one.
var next = new SecondForm();
next.Show();     // non-modal
this.Close();    // close current form after new one is visible
  • To open a modal dialog without creating a separate taskbar entry: pass the current form as owner and optionally hide it while the dialog runs.
this.Hide();
using (var dlg = new SecondForm())
{
    dlg.ShowDialog(this);   // pass owner so dialog does not get its own taskbar button
}
this.Close(); // or this.Show() if you plan to return

Practical notes and gotchas

  • Don’t cast sender to Form blindly. If you must get the form from the event sender use (sender as Control)?.FindForm() or simply use this when the handler lives in the form class.
  • Closing the startup/main form (the one passed to Application.Run) will normally exit the app. If you need to swap the main window, show the new form before closing the old one or use an ApplicationContext.
  • Avoid reparenting designer controls (like moving label1 or menuStrip1 from one form to another). A control can only have one parent; create new controls for the target form or clone values instead.

If the problem persists, confirm which form is the startup form and whether ShowDialog is called with an owner — those two facts explain almost all taskbar/visibility surprises.

Recommended Answers

All 4 Replies

Try making the CloseForm method private static void. Next at the top of the Close form method put Form frm = sender as Form(); and where you have this. in the CloseForm method swop it for frm.... see if it works.

Hi ChrisHunter, thanks for replying this post. I already tried your suggestion, but it didn't work. Maybe i put the code in wrong place or i misunderstood you, anyways it didn't work. Thanks in advance!

Can you repost the code with the changes so I can see if you misunderstood or not please?

As said, there is a language latency I hope.

As per my analysis of your code, the problem is with the following statement of your code.

if (this.InvokeRequired)
{
Action act = () =>
{
this.Hide();
};

Instead of that Hide() function use the Close() function.

Because Hide() makes the form to run at background where Close() closes the form.

Hope this helps you...

Have a happy coding...:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.