Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I was wondering how to write my C# program to Close Login Form and Show Main Form. Simply I could use Hide method for Login Form. But the Problem is, Even I close Main Form. Program doesn't Exit. to over come this issue I found a solution. I don't know whether It is good programing practise. because I'm totally new to C# and programming world.

My Solution is.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Close_Last_Form
{
    static class Program
    {  
       //Create a variabel to store result
       public static DialogResult rslt;

        //create a property to assign values to the rslt variable
        public static DialogResult rsltt
        {
            get
            {
                return rslt;
            }
            set
            {
                rslt = value;
            }
        }


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());

            //according to the rslt Open Form2 or Exit the application
            if (DialogResult.OK  == rslt)
            {
                Application.Run(new Form2());
            }
            else
            {
                Application.Exit();
            }

        }
    }
}


In my Form1. there is a button to goto the Form2. code on that Form is
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Close_Last_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //tells the Program class I'm successfull to show form2
            Program.rsltt = DialogResult.OK;
            
            Form2 Form2 = new Form2();
            Form2.Show();
            this.Close();     
        }
    }
}



I need a piece of advice from an Expert, Is this code ok to use in a program.
Thanks.
Posted
Updated 1-Aug-21 4:11am
Comments
Prasad Avunoori 15-Jul-14 0:51am    
This looks good!
Nipuna S Perera 5-Aug-14 9:19am    
Thanks for your Kind response
jo.him1988 15-Jul-14 1:22am    
or you can write this without initialize extra property
Nipuna S Perera 5-Aug-14 9:20am    
how can I do it. If you can explain, It would be great help for me

Program.cs
C#
static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
 }


Form1.cs


           private void button1_Click(object sender, EventArgs e)
        {  this.Hide();
        Form2 form2 = new Form2();
            if (form2.ShowDialog() != DialogResult.OK)
                Application.Exit();
}
 
Share this answer
 
Comments
Nipuna S Perera 28-Jul-14 0:48am    
Hi Jo.him, This is great. Thanks very much.
in Program.cs
C#
static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new frmMain());
          
       }

in Main form
C#
private void frmMain_Load(object sender, EventArgs e)
{
frmLogin frml=new frmLogin();
frml.ShowDialog();
}

in login form:
C#
private void btnLogin_Click(object sender, EventArgs e)
{
if(txtUserName.Text=="admin" && txtPassword.Text=="pass")
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Login Failed");
Application.Exit();

}
}
 
Share this answer
 
Comments
Nipuna S Perera 28-Jul-14 0:49am    
Hi Anand, This solution is also good. another way to think.
Golden Basim 5-Jun-18 19:00pm    
i have an issue :
private void btnLogin_Click(object sender, EventArgs e)
{
if(txtUserName.Text=="admin" && txtPassword.Text=="pass")
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Login Failed");
return;

}
}
but the both of conditions show home form ..
It is actually simpler than it looks.

You just need to add a code line in Main Form (Form 2 in the below given example)

Supposing We have 2 Forms Form 1 and Form 2.
Form 1 is Login Form and Form 2 is Main Form.

Pass your Parameters to open Form 2 as normally be done.

In Form 2, go to FormClosing Event and add this line.

Application.ExitThread();

Mark this as an answer if it helps. :)

Happy coding
 
Share this answer
 
Comments
Richard MacCutchan 1-Aug-21 10:24am    
This was solved seven years ago. Please try and focus on current unanswered questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900