Introduction:
I’ve created a solution (MySolution.sln) containing a project (MyProject).
Inside MyProject, I’ve created an MDI form (mdiContainer), I’ve also created a single form (frmLogin), which enable users to enter their Username and Password with two buttons (Ok and Cancel). When the user clicks the Ok button, I want to close the frmLogin window and load the mdiContainer form.
NOTE:
I haven’t yet coded the validation process to see if that user exist in the database so my no need to check for that now.
Inside MyProject, I’ve also created a class (clsBeginApplication), which basically is the starting point of my C# application. It is inside this class (clsBeginApplication) that I create the main() function.
The code:
The main() function in the clsBeginApplication class looks something like this:
[STAThread]
static void Main()
{
Application.Run(new frmLogin());
}
Now, I’ve set the clsBeginApplication class as being the “Startup Object” because it contains my main() function. When the code is executed the Application.Run fires and loads my frmLogin form. I then decide to click the Ok button and this is code inside the Ok button:
private void button1_Click(object sender, System.EventArgs e)
{
mdiContainer myMdi = new mdiContainer();
this.Close();
myMdi.Show();
}
This doesn’t seem to work, because of the “this.Close” instead of closing the frmLogin form and openning/showing the MDI form it stops my application (No error message, it just stops like if I’ve pressed the stop button in VS). What was suppose to happen was that the frmLogin would close then I would show the MDI form thus the “myMdi.Show();” piece of code.
So I said ok then, I’ll try .Hide instead of .Close so I change the code this.Close; to this.Hide and everything works great.
Questions:
1-My first question is why would .Close stop the execution of my application ?
2-If I use .Hide instead of .Close I’m assuming I’m only hiding the frmLogin but I don’t want it to be hidden I want it to be gone for ever. Is .Hide the equivalent to Me.Close like in Visual Basic ? I guess not. Is .Hide going to be cleaned up by the garbage collection later on since I’m not planning on using the frmLogin once the user presses the Ok button.
3- Also, if I use Application.Run in my class, shouldn’t I use Application.Exit to make my application stop ? I’m asking because when I used the “this.Close” it also made my application stop so I’m wondering…
4-Why would one use “Application.Run” when what I could’ve done in my clsBeginApplication class is:
[STAThread]
static void Main()
{
//Application.Run(new frmLogin());
frmLogin objFrmLogin = new frmLogin();
objFrmLogin.ShowDialog();
}
What is the difference between using one method over the other ?