here's how you would do it.
In my example, I have a main form that I want to open AFTER the user logs in correctly. This form is called "frmMain".
I also have a login form called "frmLogin" that will allow a user to login BEFORE the main from is shown.
my Program.cs file:
CODE
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// the form that you put in the following statement
// is the form that you want to open AFTER the user
// has logged in successfully.
// I am opening the main form of my application, because it is the form
// that I want to open AFTER the user logs in successfully.
Application.Run(new frmMain());
}
}
the "frmMain" form's code:
CODE
public partial class frmMain : Form
{
//Constructor of the main form
public frmMain()
{
InitializeComponent();
// this code runs BEFORE the main form is drawn to the screen
// I am opening the login screen to allow the user to login BEFORE
// the main application opens.
frmLogin loginForm = new frmLogin();
loginForm.ShowDialog();
loginForm.Dispose();
}
}
basically, when the application is started, it will start the "frmMain" which calls the constructor for the form. In the constructor, I open the "frmLogin" form.
the "frmLogin" form's code:
CODE
public partial class frmLogin : Form
{
bool authenticated = false;
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (ValidateLogin(txtUserName.Text, txtPassword.Text))
{
authenticated = true;
this.Close();
}
else
{
MessageBox.Show("Invalid login. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool ValidateLogin(string userName, string password)
{
// do your validation of the login.
// return true or false depending on whether the login was successful
}
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
if (!authenticated)
Application.Exit();
else
db.Dispose();
}
}
I use a username and password to validate the login. You do NOT have to use both if you don't want to. You can use just a password if you like.
This post has been edited by eclipsed4utoo: 1 Jan, 2009 - 12:43 PM