Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Opening a new form and closing the other one

Tags:

c#

winforms

In my program I show a login form before the main form, once the program detects a successful login I use this:

MainForm Main = new MainForm();
Main.Show();
this.Hide();

This works fine but there is a problem, the login form is still open even though it is hidden so when the program is closed the process still hangs, how can I stop this from happening?

Sorry, forgot to add, using this.Close(); doesn't work and will completely close the program.

like image 814
Dan Avatar asked Jan 23 '12 16:01

Dan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Try something more like this:

this.Hide();
Main.ShowDialog();
this.Close();

You want to hide the login form before you show the dialog, then close the login form after the dialog has been closed.

Simply closing the Login dialog will ultimately end the application, so that's not a real solution, but you still want to hide the login.

Simply put, put things in the order you want them to go in, especially when dealing with message loops.

First, you hide the login form.
Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed.
Last, once the dialog is closed, you close the login form, ending the application.

like image 149
David Morton Avatar answered Oct 19 '22 21:10

David Morton


You need to specify your MainForm when you staring application and in the Load event handler of this form ask for login. In this case you will have runned application and Login for on the starting:

Program.cs

    Application.Run(new MainForm());

MainForm.cs

    private void Form1_Load(object sender, EventArgs e)
    {
        LoginForm loginForm = new LoginForm();
        if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            // check login here
        }
    }

P.S. Close will close your application completelly if it's main form of your application.

like image 39
Samich Avatar answered Oct 19 '22 20:10

Samich