I am reading the book Charles Petzold-Programming Microsoft Windows with C# in order to learn C# and .NET so I can move away from C++ and raw WinAPI. In the book there is an example that should create 2 forms as soon as program starts.
The author writes the whole program from scratch but I wish to use designer to do this. To reiterate, when program starts 2 forms must appear.
The only thing I found so far was this thread but I am not experienced enough to figure out if that is a solution to my problem.
After reading carefully and thinking for a moment, I have modified my app like this:
1.) I have right-clicked on the name of the project in the Solution Explorer and chose Add new Item;
2.) I have added new form ( it is named Form2 );
3.) I have changed Program.cs ( the one generated for me automatically ) like below ( added relevant snippet, the rest is omitted so my post can be as brief as possible ):
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/******** this is what I have added ***********/
Form form2 = new Form2();
form2.Show();
/*********************************************/
Application.Run(new Form1());
}
When I run the program 2 forms are created. I do not notice anything unusual, like memory leaks or something similar. When I close first form the second ( form2, the one I added ) is closed too. If I close form2 first form stays.
My questions:
Since this is my first time with
C#, can you verify the correctness of my solution? If my way is not correct can you show me the proper one?How can I modify my program to close first form too, when I close the second form (I assume I need to handle second form's
WM_CLOSE)?
EDIT #1:
I have handled second form's FormClosing event and added Application.Exit(); to close first form and the entire application. I am asking if this is the right way, or is this a mistake?
EDIT #2:
I am submitting author's original code so the viewers can get the better idea of what I am trying to accomplish:
using System;
using System.Drawing;
using System.Windows.Forms;
class PaintTwoForms
{
static Form form1, form2;
public static void Main()
{
form1 = new Form();
form2 = new Form();
// omitted irrelevant property settings ( background color, text... )
form2.Show();
Application.Run(form1);
}
}
If further information is required leave a comment and I will update my post as soon as possible.