Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I made a Staff Attendance Project, but I want to set a password and user name.

How do I prevent main program form appearing before login?

And after login how to hide the login form?
Posted
Updated 13-Mar-11 6:35am
v2
Comments
Dalek Dave 13-Mar-11 12:36pm    
Edited for Readability and Grammar.

The simplest way is to handle the Form.Load event of your main form.
Add:
formLogin fl = new FormLogin();
do
   {
   if (fl.ShowDialog() == DialogResult.Cancel)
      {
      Close();
      return;
      }
   } while (!LoginOK(fl.Username, fl.Password));
That will suspend loading the main form while the user logs in, and close the form if they cancel it.

It assumes your login form has appropriate properties, and you write a LoginOK methods to check the user details.
 
Share this answer
 
Comments
Sendian 13-Mar-11 10:40am    
by the way i call login form from main project form
at load mainForm {formLogin fl = new FormLogin();} But how to handle working or hide mainForm to login
OriginalGriff 13-Mar-11 10:48am    
Follow the code I gave you: just creating the form instance does not cause it to display.
This question is asked so often that it is getting boring.

If you had searched this forum before asking you might have found Login form [^] question and it's answers and wouldn't have needaed to ask.
 
Share this answer
 
Comments
Espen Harlinn 13-Mar-11 10:42am    
You're right, it seems we have seen something like this before :)
Yusuf 13-Mar-11 11:22am    
Agree. +5
Dalek Dave 13-Mar-11 12:36pm    
Ho Ho Ho.
Sergey Alexandrovich Kryukov 13-Mar-11 15:32pm    
No wonder that most trivial Questions are most popular, my 5.
--SA
About 350 000 answers[^] - seems like somebody got this one fixed long ago :)

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 15:35pm    
Correct :-), a 5.
--SA
Espen Harlinn 13-Mar-11 16:01pm    
Thanks SAKryukov :)
private void btnOk_Click(object sender, EventArgs e)
{

txtBoxPassword.Clear();
txtBoxloginname.Clear();
this.Visible = false;
Main mainForm = new mainForm();
mainForm.FormClosed += new FormClosedEventHandler(mainForm_FormClosed);
mainForm.ShowDialog();

}


private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Visible = true;
}


This will work surely.
 
Share this answer
 
what is the problem there. to show a form you have to do is call Show

LoginForm form = new LoginForm()<br />
form.<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.show.aspx">Show</a>]();<br />

to close

form.<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx">Close</a>()
 
Share this answer
 
v2
Comments
OriginalGriff 13-Mar-11 10:48am    
No, Show() won't work: It returns immediately. He needs ShowDialog() to prevent the main form continuing unit the username/password is correct.
Yusuf 13-Mar-11 11:13am    
Absolutely right. What was I chewing?
OriginalGriff 13-Mar-11 11:15am    
I don't want to know! :laugh:
Yusuf 13-Mar-11 11:21am    
:-)
I think u need to set login form as the startup form.
u can set the startup form in Program.cs file from Solution Explorer

open Program.cs
u can see the line
Application.Run(new Form1());

u can replace Form1 with any form which has to be start first.

then if login is success then show the main page and u can hide the Login form using
Form1.Hide(); or u can close it using Form1.Close(); or Form1.Dispose();
 
Share this answer
 
v2
Comments
charles henington 14-Mar-11 14:40pm    
You would have to use Form1.Hide(); using Form1.Close if is the MainForm will Close the Application imediately after login
Wait all why you give my this ways it is so easy

when you type loginForm.Close() or .Hide() or .Dispose() it is not possible

You Have to Write
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}

You are make it so complicated
 
Share this answer
 

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