Windows Develop Bookmark and Share   
 index > Windows Forms General > loading application after logon
 

loading application after logon

I am looking to create a login screen. Once a user logs on then the main applicaiton is loaded. Can anyone point me to examples on how to do this.
Thanks.
nadim  Tuesday, September 15, 2009 7:53 PM
Hi nadim,

According to your second post's requirement. Here is a sample which can meet your need.
Main form
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}

private void FrmMain_Load(object sender, EventArgs e)
{
FrmLogin frmLogin = new FrmLogin();
if (frmLogin.ShowDialog() == DialogResult.OK)
{
// Login succeeded, do the following work
}
else
{
// Login failed
this.Close();
}
}
}

Login form
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text;
string password = txtPassword.Text;
if (Check(userName, password))
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
this.DialogResult = DialogResult.Cancel;
MessageBox.Show("User name or password is not correct");
this.Close();
}
}

private bool Check(string userName, string password)
{
// Check here. If succeeded return true, else reture false
return true;
}
}

In this sample, I have handled the Load event of FrmMain to show FrmLogin. The FrmLogin will require user name and password and do the check. If user pass the check, you can set the DialogResult to "OK". If user failed to pass the check, you can set the DialogResult to Cancel. The main form will detect whether the user has passed the cheeck based on the FrmLogin's DialogResult.

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Thursday, September 17, 2009 9:01 AM
can you tell me the requirement in detail....
if you want your app to run whenever you login, then you can make the app as startup application...
you just have to put the exe into the startup directory of windows.
goto start menu->all programs. right click and open startup.


Paras
paras kumar  Tuesday, September 15, 2009 8:05 PM


1. You run the application

2. a login form pops up.

3. you login successfully

4. login form closes

5. main application loads.

thanx.
nadim  Tuesday, September 15, 2009 8:10 PM
you can create a seperate exe for form2 and run the process from form1 and then close form1.
            if(IsLoginSuccessfull)
            {
                   Process.Start(@"form2.exe");
                   this.Close();
             }
if you dont want to create form2 as seperate exe then you can still disable and hide form1 after form2.Show()

            if(IsLoginSuccessfull)
            {                  Form2 f2 = new Form2();
                   f2.Show();
                   this.Enabled = false;
                   this.Hide();           }

Paras
paras kumar  Tuesday, September 15, 2009 8:50 PM
Hi nadim,

According to your second post's requirement. Here is a sample which can meet your need.
Main form
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}

private void FrmMain_Load(object sender, EventArgs e)
{
FrmLogin frmLogin = new FrmLogin();
if (frmLogin.ShowDialog() == DialogResult.OK)
{
// Login succeeded, do the following work
}
else
{
// Login failed
this.Close();
}
}
}

Login form
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text;
string password = txtPassword.Text;
if (Check(userName, password))
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
this.DialogResult = DialogResult.Cancel;
MessageBox.Show("User name or password is not correct");
this.Close();
}
}

private bool Check(string userName, string password)
{
// Check here. If succeeded return true, else reture false
return true;
}
}

In this sample, I have handled the Load event of FrmMain to show FrmLogin. The FrmLogin will require user name and password and do the check. If user pass the check, you can set the DialogResult to "OK". If user failed to pass the check, you can set the DialogResult to Cancel. The main form will detect whether the user has passed the cheeck based on the FrmLogin's DialogResult.

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Thursday, September 17, 2009 9:01 AM

You can use google to search for other answers

Custom Search

More Threads

• MS Word reference in .Net 2.0
• Simple mdi child mgmt
• Cannot deal with MenuStrip in inherited Form
• 'Error creating window handle'
• Why does Application.DoEvents reset the SynchronizationContext?
• paste event?
• adding hyperlink to list box items and displaying the contents of the URL in a WebBrowser control
• loading colored cursor image
• MDI "popout" windows
• WTL CComboBox and Tooltip