What is the best way to close a form and get to another form.
At present I have a Main Form and then two forms:
I want to know how to close one form and open another form efficiently.The two times I did it had a slight different outcome:
The main form in my application is just the introduction which will load some Company Logo and show a progress bar which will then take it to a log-in form, where in I have a log-in button, from where the actual application will open.
I have some questions.In the Main Form I have added this code:
private void Form1_Load(object sender, EventArgs e) { int i; for (i = 0; i <= 100; i++) { progressBar.Value = i; label1.Text = "Please wait while Refresh loads up..."; } } private void progressTimer_Tick(object sender, EventArgs e) { this.Close(); } private void MainForm_Deactivate(object sender, EventArgs e) { Form form = new FirstForm(); form.ShowDialog(this); }
1) This works fine, just that the new form that opens up is at the task bar and is not in the center of the screen(as I have set it’s property).How do I fix this?
In the First Form I have added this code:
private void loginButton_Click(object sender, EventArgs e) { using( ERPQueries eq = new ERPQueries()) { int? count = eq.CheckEmployee(userTextBox.Text,passwordTextBox.Text); if (count == 1) { //testLabel.Text = "Ok"; this.Close(); Form form = new SecondForm(); form.ShowDialog(this); } else { testLabel.Text = "Invalid username or password!"; } } }
2) Here the next Form pops up in the center of the screen.I want to know how is it different from the first case, as I have used showDialog() in both the cases?
3) Also in the first case my Main Form Disappears, whereas in the second case the First Form is still visible in the background and disappears only after closing the SecondForm.
I’m sure I’m doing a lot of mistakes, my code is flawed.Please help.This is the first time I’m making an application with multiple forms.
Edit:
When I use Show() instead of ShowDialog() I don’t see the new form.Am I missing something?
I tried this.Dispose() instead of this.Close().In the first case it works fine.In the second case, it disposes all the forms.
try:
not
which is what makes it modal.