Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
C#
private void frmMain_Load(object sender, EventArgs e)
        {
            objFrmLogin = new frmLogin();
            DialogResult mResult = new DialogResult();
            do
            {
                mResult = objFrmLogin.ShowDialog(this);
            }(while mResult!=DialogResult.OK);
        }


C#
private void btnLogin_Click(object sender, EventArgs e)
        {
            objUser.Username = this.txtUsername.Text;
            objUser.Password = this.txtPassword.Text;
            objDB.OpenDB();
            if (objUser.Verify() == true)
            {
                ((frmMain)(this.Owner)).lblCUserName.Text = objUser.Username;
                this.DialogResult = DialogResult.OK;
                this.Close();            
            }
            else
            {
                MessageBox.Show("Either UserName or Password is incorrect.", "Master Industrial Concerns", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.Cancel;
            }
        }


Thankx guys for your suggestions. I have modified the code as above code. Its working now according to my requirement. Is this approach is good or bad?

Thankx for helping me
Posted
Updated 16-Jul-10 21:43pm
v2
Comments
Nish Nishant 15-Jul-10 9:04am    
Reason for my vote of 5
Well phrased. 5!

If the DialogResult property of your button is set to something at all, it will close the login dialog when the button is pressed.

I'm not 100% sure, but I think the same is the case if your button is set as AcceptButton or CancelButton.

So either you change those properties or you do a nasty hack: Set a boolean variable with the result when you check the login.

Then in your login form's FormClosing event you check if the result is ok. If NOT, set e.Cancel to true. That will keep your form open.

Good luck...
 
Share this answer
 
v2
Comments
Johnny J. 15-Jul-10 8:56am    
BTW: You shouldn't close your login form (or any form at all) by using this.Dispose. That way you will not be able to access properties from the main form after it's closed (if you should want to do that). Use this.Close() instead (and if you absolutely want to explicitly dispose of it - which is not necessary if you trust the GC - then you do it from your main form afterwards. But it will be automatically disposed of when it's scope runs out, e.g. when frmMain_Load is exited)
Nish Nishant 15-Jul-10 9:05am    
Reason for my vote of 5
Good answer. Worth 5.
JSOP posted this[^] Tip/Trick about a clean way to show a login form. Basically it's a simple pattern for handling the login process in Main before showing the main form. With a little restructuring you can maintain the separation of responsibilities of the main and login forms in your app and in the process eliminate the awkward synchronization between them.

If the login form is responsible for
* offering the option to exit completely
* gathering user login info
* validating the info
* if invalid info is entered stay open and ask them again

then Main can use it to wait until the user enters valid login info or the user decides to give up trying to remember their forgotten password. If what they enter is valid then Main can pass the user's information to the main form when it instantiates and runs it.
 
Share this answer
 
Comments
Steven A. Lowe 15-Jul-10 14:13pm    
Reason for my vote of 5
login first, then show main
Member 4104703 17-Jul-10 11:16am    
Reason for my vote of 5
the best place to check the login credentilas is the Main. In Main, call a private method to do it, no problems in keeping not needed session.
You have put the login code in form_load; I guess it's too late! the form is already created & opened.
Try putting it before the form is opened(in the Main method)
 
Share this answer
 
You could try setting the TopMost property for the child form.
 
Share this answer
 
Comments
Johnny J. 15-Jul-10 8:47am    
Reason for my vote of 1
That's not going to work if the form is closed, is it....???

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