Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
In a Login form

After filling UserName & Password

then i want Open my project without any button click

so what should i do...
Posted
Comments
DamithSL 2-Nov-14 1:46am    
Ok, my first question will be, how the system decide that user has entered both username and password and now ready to open the project?

there should be a proper event like both username and password not empty and user click on keyboard enter key..

update the question and clearly add what are the condition and how system decide when to open project etc..
Sergey Alexandrovich Kryukov 2-Nov-14 1:50am    
In addition to the above: there is no such thing as "open a project"; during runtime, there is no a "project" anymore; it's left behind after you built it.
The question doesn't really make sense. If you mean that you just want to continue, use handle KeyDown event.
—SA
BillWoodruff 2-Nov-14 2:20am    
Hi Sergey, I think in this case it's obvious the OP wants to present a log-in Form, and, if log-in is successful, move on to creating/showing his other (Main, and whatever) Form(s).
Sergey Alexandrovich Kryukov 2-Nov-14 15:53pm    
Sure. I just advise to understand carefully what is what. Confusing development artifacts (projects, etc.) with build output (and what is deployed) is a common problem. Sorting it out is important enough.
Thank you.
—SA

1 solution

I'll show you how to do this in Windows Forms, but you should understand that doing things this way:

1. is unusual: who knows how Windows Forms may change in the future in a way that would make this technique cause an error. in fact, I've deliberately used every rather unusual technique I know in this method (for the fun of it) :)

2. as long as the username and password are in "plain text" anywhere, in any program, anyone with some technical skill, and the motivation, is going to be able to hack and get access to the plain text. so: don't think this technique is any kind of strong "protection: there is no substitute for using encryption, and Windows Security services, and your databases security facilities !

How to do this:

0. create your Application in the usual way: we will modify the 'Program file used to start the Application in later steps. I'll assume your Main Form is named 'MainForm.

1a. Create a LogIn Form, 'LogInForm, with two TextBoxes: tbUserName, tbPassword

set 'tbPassword's UseSystemPasswordChar property to 'true, or set its 'PasswordChar property to a character you choose.

1b. do the right thing to set the appeaerance/behavior properties of the the log-in Form and its TextBoxes so they reflect your design. for example, I'd choose to set the 'ShowInTaskBar property of the log-in Form to false.

2. Open the Program.cs file of your WinForm solution, and modify like this:
C#
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            lgIn = new Login();
            lgIn.ShowDialog();

            if (go)
            {
                Application.Run(new MainForm());
            }
        }

        public static Login lgIn;

        public static bool go = false, c1 = false, c2 = false;

        public static void TX(object sender, EventArgs eventArgs)
        {
            var t = sender as TextBox;

            switch (t.Name)
            {
                case "tbUserName":
                    c1 = t.Text == "username";
                    break;
                case "tbPassword":
                    c2 = t.Text == "password";
                    break;
            }

            if (c1 && c2)
            {
               go = true;
               lgIn.Dispose();
            }
        }
    }
}
3. Code the LogIn form:
C#
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();

            tbPassword.TextChanged += (obj, eargs) => Program.TX(tbPassword, eargs);
            tbUserName.TextChanged += (obj, eargs) => Program.TX(tbUserName, eargs);

            this.FormClosed += (obj, eargs) =>
            {
                MessageBox.Show((Program.go)
                    ? "You have logged in."
                    : "You have not logged in.");
            };
        }
    }
}
How does this "work:"

1. code in the Program.cs static Main method is executed:

a. a new LogIn Form is created and displayed using 'ShowDialog which pauses the main thread ... stops the show.

b. in the Program Class a method that can be used as an EventHandler for TextBox TextChanged is defined: 'TX.

2. when the Login Form is created:

a. event handlers are assigned for the 'TextChanged Event of both TextBoxes: both of these invoke the 'TX event handler defined in Program.cs, which is static.

b. an event handler is assigned for the LogIn form's 'Closed Event. in that event code, the Program.cs static boolean variable 'go is used to present a message to the user telling them whether or not they are logged-in.

3. as the user types in the two TextBoxes on the LogIn Form:

a. the 'TX event handler in Program.cs is called with each character entered: a test is done to see if the log-in is complete.

4. when the LogIn is successfully completed:

a. the Disposing of the LogIn form unblocks the main thread.

b. then the Application is started in the usual way: the MainForm is displayed.
 
Share this answer
 
Comments
George Jonsson 2-Nov-14 5:40am    
Having a boring day? :)
+5 for the teaching effort.
BillWoodruff 2-Nov-14 6:13am    
Any day I can pretend I have a life is not a boring day, George, and thanks for the vote !
George Jonsson 2-Nov-14 6:20am    
Well put.
george4986 3-Nov-14 1:07am    
nice explanation ;-)

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