942,963 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1578
  • C# RSS
Nov 9th, 2009
0

Login form appears before (parent) Form1

Expand Post »
I have a form1 as a parent form (and is a MDI container) which is a parent for a lot of the other child forms. Now I would like to create a Login. It should be another windows form - formLogin called. And it has to start the 1st when application is started (so it has to start before form1). Is this possible?

--------------------------------------------------------------------------------------
What I did for a test:
I tryed to set FormLogin as a parent (and MDI contrainer = true), and form1 (which it wasn`t any more a parent, and not even a MDI) and I set in the program.cs file: Application.Run(new FormLogin());
The formLogin is started, then it goes through the login code, if the username and pw are correct I simply did:
C# Syntax (Toggle Plain Text)
  1. Fom1 = new Form1();
  2. form1.MdiParent = this;
  3. form1.Show();

But it does not open. I don`t know why.
----------------------------------------------------
But I believe this is not the correct way, it can not be the loginForm a parent, it should be the Form1.
Last edited by peter_budo; Nov 9th, 2009 at 12:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 9th, 2009
0
Re: Login form appears before (parent) Form1
Your Application.Run should run your parent form. Application.Run(new Form1());
in your Form1 FormLoad event you create and show the login form. By showing it in the form load it will appear before the parent:

C# Syntax (Toggle Plain Text)
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. FormLogin login = new FormLogin();
  4. login.MdiParent = this;
  5. login.ShowDialog();
  6. }

By calling ShowDialog() you block Form1 until the login is finished so Form1 wont appear until you close/return the login form.
You will probably want to include a public property in the Login Form to allow Form1 to retreive the login credentials.
You can also put code in the form load to process the login before the MDI Parent is shown; show certain controls based on user credentials etc.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 9th, 2009
0
Re: Login form appears before (parent) Form1
Click to Expand / Collapse  Quote originally posted by Ryshad ...
You can also put code in the form load to process the login before the MDI Parent is shown; show certain controls based on user credentials etc.
How to do that?

This is what I did so far in LoginForm:
C# Syntax (Toggle Plain Text)
  1. private void buttonLogin_Click(object sender, EventArgs e)
  2. {
  3. string username = textBoxIme.Text;
  4. string password = textBoxGeslo.Text.Trim();
  5. CheckingLogin(username, password);
  6. //What to write here when code returns from CheckLogin methnd?
  7. //If returns with true it has to close() LoginForm and show Form1
  8. //Else if returns with false, it has to show loginForm ones again (and not allow to go into Form1)
  9.  
  10. }
  11.  
  12. static string sql_Conn = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;
  13. public static bool ChecingLogin(string userName, string password)
  14. {
  15. bool myResult = false;
  16.  
  17. //The rest of the login form, which checks username with pw
  18. }
Last edited by Mitja Bonca; Nov 9th, 2009 at 7:26 am.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 9th, 2009
0
Re: Login form appears before (parent) Form1
Firstly, i just realised a mistake in my original code. You need to remove login.MdiParent = this; as you cant open a child form modally.

Rather than closing the form when they have logged in you will want to hide it so you can still retrieve the username/password entered:

C# Syntax (Toggle Plain Text)
  1. string username = textBoxIme.Text;
  2. string password = textBoxGeslo.Text.Trim();
  3. if(CheckingLogin(username, password)==true)
  4. {
  5. this.Hide();
  6. }
  7. else
  8. {
  9. MessageBox.Show("Invalid username/password");
  10. }

There is a very good guide by sknake available that you should look at: Sample C# Windows Form Login Screen
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 9th, 2009
0
Re: Login form appears before (parent) Form1
Thank you, just one more thing:
Did it:
C# Syntax (Toggle Plain Text)
  1. string username = textBoxIme.Text;
  2. string password = textBoxGeslo.Text.Trim();
  3. if (CheckingLogin(username, password) == true)
  4. {
  5. //if its ok
  6. }
btw, what to put in the brackets if I did in Program.cs:
C# Syntax (Toggle Plain Text)
  1. using (LoginForm login = new LoginForm())
  2. {
  3. if (login.ShowDialog() == DialogResult.OK)
  4. Application.Run(new Form1());
  5. }
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 9th, 2009
0
Re: Login form appears before (parent) Form1
If you wanted to do it that way you would need to set the LoginForms dialogresult property like this:

C# Syntax (Toggle Plain Text)
  1. if (CheckingLogin(username, password) == true)
  2. {
  3. this.DialogResult = DialogResult.OK;
  4. }

Just be aware that if they enter the wrong details and close the login form then the application will close.
You might want to add in a finite loop which allows 3 attempts before the application closes.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Creating a 3D desktop with C#
Next Thread in C# Forum Timeline: Reading data from an access database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC