the proper way to make a login screen appear in the main class - Main() - program.cs
-
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 regardsBad = knowing 2 much
-
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 regardsBad = knowing 2 much
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.
-
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 regardsBad = knowing 2 much
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.
-
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.
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
-
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.
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
-
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
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.
-
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.
// 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 withthis.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 normalpublic bool Succeeded = false;
in combination ofLoginscreen.LoginScreen login = new Loginscreen.LoginScreen() ;
doesn't work, the class gets disposed after the login screen gets closed andpublic 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 withApplication.Run(new loginscreen.LoginScreen());
after the login screen gets closed bythis.Close();
or the RED X?Bad = knowing 2 much
-
// 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 withthis.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 normalpublic bool Succeeded = false;
in combination ofLoginscreen.LoginScreen login = new Loginscreen.LoginScreen() ;
doesn't work, the class gets disposed after the login screen gets closed andpublic 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 withApplication.Run(new loginscreen.LoginScreen());
after the login screen gets closed bythis.Close();
or the RED X?Bad = knowing 2 much
[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.”
-
// 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 withthis.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 normalpublic bool Succeeded = false;
in combination ofLoginscreen.LoginScreen login = new Loginscreen.LoginScreen() ;
doesn't work, the class gets disposed after the login screen gets closed andpublic 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 withApplication.Run(new loginscreen.LoginScreen());
after the login screen gets closed bythis.Close();
or the RED X?Bad = knowing 2 much
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.
-
[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.”
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 useDialogResult.OK
again. what is the advantage from your code over mine?Bad = knowing 2 much
-
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 useDialogResult.OK
again. what is the advantage from your code over mine?Bad = knowing 2 much
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 theCancelButton
, and don't forget to set theDialogResult
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.”
-
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.
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