1

i have a problem:

Here is my case:

i already command a NewForm to be called everytime the user select that. The MainForm is being called when i run the program, and when i click "NewForm", the FirstNewForm being called and the MainForm is closed. But, when i click "NewForm" again in the FirstNewForm that i had created, it called the SecondNewForm, but the FirstNewForm is not being closed.

Here is my question:

How do i close the FirstNewForm? i mean, how do i close a CurrentForm whenever i called a NewForm?

Here is the image when i command a "NewForm" when the MainForm still in there, and MainForm is closed when FirstNewForm being called: (if you see in the taskbar, just one Selling System Program in there): (The first image is MainForm, and the second image is FirstNewForm, it looked the same, because the MainForm already closed when i called a FirstNewForm):

enter image description here

enter image description here

Here is the image when i command a "NewForm" when the FirstNewForm still in there, and FirstNewForm is not being closed when SecondNewForm being called: (if you see in the taskbar,there are two Selling System Programs in there, so the FirstNewForm is not being closed)

enter image description here

How do i hide and close the FirstNewForm?

Here is the code:

private void AddNewForm(object sender, EventArgs e)
    {
        //this.Hide();
        Form newForm = new Form();
        AddObjects(sender, e, newForm);
        UpdateTextPosition(sender, e, newForm);

        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.CenterScreen;
        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);

        //newForm.ShowDialog();

        //this.Close();

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

          this.Invoke(act);
        }

        else
        {
          this.Hide();
        }
    }

Thanks in advance! and sorry if i am post a long posting

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
Reinhardt
  • 145
  • 13
  • 2
    You ask an awful lot of questions in a short period of time: 1. http://stackoverflow.com/questions/18348480/text-not-all-displayed-by-the-time-new-form-has-been-created 2. http://stackoverflow.com/questions/18338451/click-an-enter-button-to-create-a-new-textbox 3. http://stackoverflow.com/questions/18332405/create-a-new-form-every-time-a-button-has-been-clicked and now this one, can you at least try to figure it something on your own? – Max Aug 21 '13 at 07:47
  • @MaxMommersteeg: well, i am sorry sir MaxMommersteeg, because i asked a lot of questions – Reinhardt Aug 21 '13 at 09:23
  • @HabibZare: on my experience, Show() function is to appear the program just a seconds only, not permanent.. and i dont want that – Reinhardt Aug 21 '13 at 09:23

2 Answers2

1

ShowDialog causes a modal dialog to show. This means, the call newForm.ShowDialog() will block and this.Close() will only be reached AFTER you close the new form. You could call this.Hide() on the main form to make it invisible and close all forms together when you exit your program:

this.Hide();

newForm.ShowDialog();

this.Close();

EDIT

It might appear that your call to this.Hide() is not properly dispatched to the UI-thread and so it refuses to hide the form. Try

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

'NOTHER EDIT

private void AddNewForm(object sender, EventArgs e)
{
    ...

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

      this.Invoke(act);
    }

    else
    {
      this.Hide();
    }

    newForm.ShowDialog();

}
bash.d
  • 13,029
  • 3
  • 29
  • 42
  • He calls `this.Hide()` at the top of the method – El Ronnoco Aug 21 '13 at 07:32
  • we can use this.Close(); before newForm.ShowDialog(); – Habib Zare Aug 21 '13 at 07:35
  • 1
    No, you can't. If it is the main Window, you will terminate the process! – bash.d Aug 21 '13 at 07:37
  • hello @bash.d: i already tried the above code, (while i create a NewForm in MainForm, the MainForm and the NewForm closed together, but the NewForm cannot be opened or the program direct close when i click a "New" to create a new form) Thanks – Reinhardt Aug 21 '13 at 09:24
  • Please refer to my question code, i already update it @bash.d. Thanks – Reinhardt Aug 21 '13 at 09:30
  • @bash.d: the firstnewform still on there when i called secondnewform. I have been tried "newForm.Hide(); and then newForm.ShowDialog(); and then newForm.Close();", but the firstnewform still on there when i called secondnewform. Thank you so much – Reinhardt Aug 21 '13 at 09:38
  • @bash.d: i am sorry, what do you mean by proceed exactly like with the main window? i dont get it – Reinhardt Aug 21 '13 at 10:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35913/discussion-between-fuhans-and-bash-d) – Reinhardt Aug 21 '13 at 11:54
  • No, I won't, sorry... Got other things to spend my time on. – bash.d Aug 21 '13 at 12:10
0

On the mainform You must assign to both childforms eventhandlers OnFormClosed. There You can react properly and control each child forms behaviour as it should be done.

icbytes
  • 1,831
  • 1
  • 17
  • 27