Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. the proper way to make a login screen appear in the main class - Main() - program.cs

the proper way to make a login screen appear in the main class - Main() - program.cs

Scheduled Pinned Locked Moved C#
tutorial
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Demon
    wrote on last edited by
    #1

    this code below, show first 1 screen and if you close 'login screen' the 'new Form1' gets created and shown.

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new loginscreen.LoginScreen());
            Application.Run(new Form1());
        }
    

    the code below works fine, but i would like to know witch property of the 'login' class i need to detect if the login window has been closed and finalize the class and then open the main window of the application (new Form1).

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            loginscreen.LoginScreen login = new loginscreen.LoginScreen();
            login.Show();
    
            if(login. // need work here
            {
                 Application.Run(new Form1());
            }
            else
            {
                // login failed after 3 attemps, close the whole app
                Application.Exit();
            }
        }
    

    how to detect or witch property of the login class tells me the login window is closed. kind regards

    Bad = knowing 2 much

    C P 2 Replies Last reply
    0
    • N Natural_Demon

      this code below, show first 1 screen and if you close 'login screen' the 'new Form1' gets created and shown.

          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new loginscreen.LoginScreen());
              Application.Run(new Form1());
          }
      

      the code below works fine, but i would like to know witch property of the 'login' class i need to detect if the login window has been closed and finalize the class and then open the main window of the application (new Form1).

          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              loginscreen.LoginScreen login = new loginscreen.LoginScreen();
              login.Show();
      
              if(login. // need work here
              {
                   Application.Run(new Form1());
              }
              else
              {
                  // login failed after 3 attemps, close the whole app
                  Application.Exit();
              }
          }
      

      how to detect or witch property of the login class tells me the login window is closed. kind regards

      Bad = knowing 2 much

      C Offline
      C Offline
      CoderForEver
      wrote on last edited by
      #2

      In my opinion ..... Set a variable then if the login is not succesful increment that variable .... if it is greater than 3 ...... call application.exit(); ..... Else call the other form.

      N 1 Reply Last reply
      0
      • N Natural_Demon

        this code below, show first 1 screen and if you close 'login screen' the 'new Form1' gets created and shown.

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new loginscreen.LoginScreen());
                Application.Run(new Form1());
            }
        

        the code below works fine, but i would like to know witch property of the 'login' class i need to detect if the login window has been closed and finalize the class and then open the main window of the application (new Form1).

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                loginscreen.LoginScreen login = new loginscreen.LoginScreen();
                login.Show();
        
                if(login. // need work here
                {
                     Application.Run(new Form1());
                }
                else
                {
                    // login failed after 3 attemps, close the whole app
                    Application.Exit();
                }
            }
        

        how to detect or witch property of the login class tells me the login window is closed. kind regards

        Bad = knowing 2 much

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Use ShowDialog instead ? Or more like your first example, Run the login form. In either case, you can have a public property on your login form to indicate a successful login. Were I doing that, I'd have the property return something that Form1 will require to run. You wouldn't want someone to use Reflection to run your app without logging in.

        N 1 Reply Last reply
        0
        • C CoderForEver

          In my opinion ..... Set a variable then if the login is not succesful increment that variable .... if it is greater than 3 ...... call application.exit(); ..... Else call the other form.

          N Offline
          N Offline
          Natural_Demon
          wrote on last edited by
          #4

          well, i know how to do that. i wanna know how to detect if a form closed in Main(). if i press the RED X of login and return to main() and continue or exit. thank you and kind regards

          Bad = knowing 2 much

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Use ShowDialog instead ? Or more like your first example, Run the login form. In either case, you can have a public property on your login form to indicate a successful login. Were I doing that, I'd have the property return something that Form1 will require to run. You wouldn't want someone to use Reflection to run your app without logging in.

            N Offline
            N Offline
            Natural_Demon
            wrote on last edited by
            #5

            in the first example, how do i know, i closed the thread or 'Login' form? and resume with Main(). in the second example, i wanna know, witch property of the login class tells me that the login window is closed and i can resume in Main() and decide wether to proceed or close. i don't wanna show Form1 until you true login form. in the first example, login window gets displayed and if i close it, Form1 gets displayed. witch on it self is good, but how do i tell Main() the user past it test or not and proceed or close. kind regards

            Bad = knowing 2 much

            P 1 Reply Last reply
            0
            • N Natural_Demon

              in the first example, how do i know, i closed the thread or 'Login' form? and resume with Main(). in the second example, i wanna know, witch property of the login class tells me that the login window is closed and i can resume in Main() and decide wether to proceed or close. i don't wanna show Form1 until you true login form. in the first example, login window gets displayed and if i close it, Form1 gets displayed. witch on it self is good, but how do i tell Main() the user past it test or not and proceed or close. kind regards

              Bad = knowing 2 much

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Natural_Demon wrote:

              how do i tell Main() the user past it test or not

              As I said, the login form can have a public property of your devising that will tell it:

              Form login = new Login() ;

              Application.Run ( login ) ;

              if ( login.Succeeded )
              {
              Application.Run ( new Form1 ( login.UserInfo ) ) ;
              }

              or some such technique.

              N 1 Reply Last reply
              0
              • P PIEBALDconsult

                Natural_Demon wrote:

                how do i tell Main() the user past it test or not

                As I said, the login form can have a public property of your devising that will tell it:

                Form login = new Login() ;

                Application.Run ( login ) ;

                if ( login.Succeeded )
                {
                Application.Run ( new Form1 ( login.UserInfo ) ) ;
                }

                or some such technique.

                N Offline
                N Offline
                Natural_Demon
                wrote on last edited by
                #7

                // program.cs

                using System;
                using System.Collections.Generic;
                using System.Windows.Forms;

                namespace Modales_window
                {
                static class Program
                {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new loginscreen.LoginScreen());
                if (loginscreen.LoginScreen.Succeeded)
                {
                Application.Run(new Form1());
                }
                else
                {
                Application.Exit();
                }
                }
                }
                }

                LoginScreen.cs

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Drawing;
                using System.Data;
                using System.Text;
                using System.Windows.Forms;

                namespace loginscreen
                {
                public partial class LoginScreen : Form
                {
                static public bool Succeeded = false;
                public LoginScreen()
                {
                InitializeComponent();
                }

                    private void button1\_Click(object sender, EventArgs e)
                    {
                        Succeeded = true;     
                        this.Close();
                    }
                }
                

                }

                this seams to do the job perfectly. if you click the button in the login screen, the bool gets set to true and the windows gets closed with this.Close();, making the main Form appear, if you close the login form with the RED X, the application closes. : ) i tried your sollution, but a normal public bool Succeeded = false; in combination of Loginscreen.LoginScreen login = new Loginscreen.LoginScreen() ; doesn't work, the class gets disposed after the login screen gets closed and public bool Succeeded = true; gets lost/disposed and the application exit's. after a litle thinking, i tried to make it static and without the creation of an instance of the login class. 1 more question, .... what happens with Application.Run(new loginscreen.LoginScreen()); after the login screen gets closed by this.Close(); or the RED X?

                Bad = knowing 2 much

                H L 2 Replies Last reply
                0
                • N Natural_Demon

                  // program.cs

                  using System;
                  using System.Collections.Generic;
                  using System.Windows.Forms;

                  namespace Modales_window
                  {
                  static class Program
                  {
                  /// <summary>
                  /// The main entry point for the application.
                  /// </summary>
                  [STAThread]
                  static void Main()
                  {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.Run(new loginscreen.LoginScreen());
                  if (loginscreen.LoginScreen.Succeeded)
                  {
                  Application.Run(new Form1());
                  }
                  else
                  {
                  Application.Exit();
                  }
                  }
                  }
                  }

                  LoginScreen.cs

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Drawing;
                  using System.Data;
                  using System.Text;
                  using System.Windows.Forms;

                  namespace loginscreen
                  {
                  public partial class LoginScreen : Form
                  {
                  static public bool Succeeded = false;
                  public LoginScreen()
                  {
                  InitializeComponent();
                  }

                      private void button1\_Click(object sender, EventArgs e)
                      {
                          Succeeded = true;     
                          this.Close();
                      }
                  }
                  

                  }

                  this seams to do the job perfectly. if you click the button in the login screen, the bool gets set to true and the windows gets closed with this.Close();, making the main Form appear, if you close the login form with the RED X, the application closes. : ) i tried your sollution, but a normal public bool Succeeded = false; in combination of Loginscreen.LoginScreen login = new Loginscreen.LoginScreen() ; doesn't work, the class gets disposed after the login screen gets closed and public bool Succeeded = true; gets lost/disposed and the application exit's. after a litle thinking, i tried to make it static and without the creation of an instance of the login class. 1 more question, .... what happens with Application.Run(new loginscreen.LoginScreen()); after the login screen gets closed by this.Close(); or the RED X?

                  Bad = knowing 2 much

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

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

                  		LoginForm lf = new LoginForm();
                  		if (lf.ShowDialog() == DialogResult.OK)
                  		{
                  			// do stuff to test good login here
                  			Application.Run(new MainForm());
                  		}
                  		else
                  		{
                  			Application.Exit();
                  		}
                               }
                  

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  N 1 Reply Last reply
                  0
                  • N Natural_Demon

                    // program.cs

                    using System;
                    using System.Collections.Generic;
                    using System.Windows.Forms;

                    namespace Modales_window
                    {
                    static class Program
                    {
                    /// <summary>
                    /// The main entry point for the application.
                    /// </summary>
                    [STAThread]
                    static void Main()
                    {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new loginscreen.LoginScreen());
                    if (loginscreen.LoginScreen.Succeeded)
                    {
                    Application.Run(new Form1());
                    }
                    else
                    {
                    Application.Exit();
                    }
                    }
                    }
                    }

                    LoginScreen.cs

                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.Drawing;
                    using System.Data;
                    using System.Text;
                    using System.Windows.Forms;

                    namespace loginscreen
                    {
                    public partial class LoginScreen : Form
                    {
                    static public bool Succeeded = false;
                    public LoginScreen()
                    {
                    InitializeComponent();
                    }

                        private void button1\_Click(object sender, EventArgs e)
                        {
                            Succeeded = true;     
                            this.Close();
                        }
                    }
                    

                    }

                    this seams to do the job perfectly. if you click the button in the login screen, the bool gets set to true and the windows gets closed with this.Close();, making the main Form appear, if you close the login form with the RED X, the application closes. : ) i tried your sollution, but a normal public bool Succeeded = false; in combination of Loginscreen.LoginScreen login = new Loginscreen.LoginScreen() ; doesn't work, the class gets disposed after the login screen gets closed and public bool Succeeded = true; gets lost/disposed and the application exit's. after a litle thinking, i tried to make it static and without the creation of an instance of the login class. 1 more question, .... what happens with Application.Run(new loginscreen.LoginScreen()); after the login screen gets closed by this.Close(); or the RED X?

                    Bad = knowing 2 much

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Natural_Demon wrote:

                    doesn't work, the class gets disposed after the login screen gets closed

                    I think that is correct. There probably are many ways to achieve what you want. Here are two ways I would try: 1. Use a modal dialog for the login stuff; this is the logical approach as I assume the MainForm isn't going to do any useful work if you're not logged in. Use the DialogResult value to indicate success. Modal dialogs need disposing, so a using statement is used.

                    bool loggedIn=false;
                    using (LoginForm lf=new LoginForm()) loggedIn=lf.ShowDialog()==DialogResult.OK;
                    if (loggedIn) Application.Run(new MainForm());

                    2. However if MainForm could do useful things while user is looking around/typing name&password:

                    MainForm mf=new MainForm(); // constructor can launch thread to perform some harmless work; nothing shows yet.
                    bool loggedIn=false;
                    using (LoginForm lf=new LoginForm()) loggedIn=lf.ShowDialog()==DialogResult.OK;
                    if (loggedIn) Application.Run(mf); //the implicit mf.Show() will cause Load and Shown events

                    :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    N 1 Reply Last reply
                    0
                    • H Henry Minute

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

                      		LoginForm lf = new LoginForm();
                      		if (lf.ShowDialog() == DialogResult.OK)
                      		{
                      			// do stuff to test good login here
                      			Application.Run(new MainForm());
                      		}
                      		else
                      		{
                      			Application.Exit();
                      		}
                                   }
                      

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      N Offline
                      N Offline
                      Natural_Demon
                      wrote on last edited by
                      #10

                      indeed, this does also work, after you set a AcceptButton property in the login screen/Form. anyway, i had my code working. you helped remember how to use DialogResult.OK again. what is the advantage from your code over mine?

                      Bad = knowing 2 much

                      H 1 Reply Last reply
                      0
                      • N Natural_Demon

                        indeed, this does also work, after you set a AcceptButton property in the login screen/Form. anyway, i had my code working. you helped remember how to use DialogResult.OK again. what is the advantage from your code over mine?

                        Bad = knowing 2 much

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #11

                        No real advantage, just less coding all round, saves worrying about declaring the boolean member and setting it in the button click. As well as the AcceptButton, there is the CancelButton, and don't forget to set the DialogResult property for the buttons, if you use this method.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          Natural_Demon wrote:

                          doesn't work, the class gets disposed after the login screen gets closed

                          I think that is correct. There probably are many ways to achieve what you want. Here are two ways I would try: 1. Use a modal dialog for the login stuff; this is the logical approach as I assume the MainForm isn't going to do any useful work if you're not logged in. Use the DialogResult value to indicate success. Modal dialogs need disposing, so a using statement is used.

                          bool loggedIn=false;
                          using (LoginForm lf=new LoginForm()) loggedIn=lf.ShowDialog()==DialogResult.OK;
                          if (loggedIn) Application.Run(new MainForm());

                          2. However if MainForm could do useful things while user is looking around/typing name&password:

                          MainForm mf=new MainForm(); // constructor can launch thread to perform some harmless work; nothing shows yet.
                          bool loggedIn=false;
                          using (LoginForm lf=new LoginForm()) loggedIn=lf.ShowDialog()==DialogResult.OK;
                          if (loggedIn) Application.Run(mf); //the implicit mf.Show() will cause Load and Shown events

                          :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                          N Offline
                          N Offline
                          Natural_Demon
                          wrote on last edited by
                          #12

                          indeed, your reply is very usefull to me. i'm making an agenda for a client. (it's my first attemp to sell a program to someone and i want to be as good as possible) i could keep the mainform running and after some time lock the mainform again. to prevent other people from messing the agenda or data.

                          Bad = knowing 2 much

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups