Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to validate the username and password in winformm using visual c#.


in program.cs class,
C#
 static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            if(Username==abcd amp;& Password==abcd)
            {
                DialogResult=DialogResult.OK;

            }
            else
            {
                MessageBox.Show("invalid Username and password);
            }

        }
    }
}
i want to validate the login page , when i enter the username as "abcd" and 
password "abcd" it would show next form if it is wrong it show message box as wrong
Posted

Don't do it there. The validation code will only ever be executed when the form is closed - which terminates your application.

In your normal Main form, edit the Load event handler and display your login form from that using the ShowDialog method.
If the validation fails, show the messagebox and call Application.Exit.
If it passes, just continue and your main form will be displayed.
 
Share this answer
 
Comments
Xaavier 29-Aug-15 5:45am    
can you please share the code....
OriginalGriff 29-Aug-15 6:05am    
You are kidding, right? :laugh:
You know how to handle an event, you know how to create a form instance, you know how to use ShowDialog.
So what code do you want me to write that you can't?
1.call loginform and flipped the MainForm instance in the argument.
2.if login succ in loginform , show MainForm
3.fail - > application.exit();

http://csdp000.org/?p=154
namespace test  
{
    class LoginForm : Form
    {
        private Form _mainForm; // Main Form Instance

        private TextBox pwTextBox;
        private Button loginButton;
        private TextBox idTextBox; 

        public LoginForm(Form form)
        {
            InitializeComponent();
            _mainForm = form; 
        }

        private void InitializeComponent()
        {
            this.idTextBox = new System.Windows.Forms.TextBox();
            this.pwTextBox = new System.Windows.Forms.TextBox();
            this.loginButton = new System.Windows.Forms.Button();
            this.SuspendLayout(); 

            this.idTextBox.Location = new System.Drawing.Point(12, 12);
            this.idTextBox.Name = "idTextBox";
            this.idTextBox.Size = new System.Drawing.Size(159, 21);
            this.idTextBox.TabIndex = 0; 

            this.pwTextBox.Location = new System.Drawing.Point(12, 39);
            this.pwTextBox.Name = "pwTextBox";
            this.pwTextBox.Size = new System.Drawing.Size(159, 21);
            this.pwTextBox.TabIndex = 1; 

            this.loginButton.Location = new System.Drawing.Point(177, 10);
            this.loginButton.Name = "loginButton";
            this.loginButton.Size = new System.Drawing.Size(121, 50);
            this.loginButton.TabIndex = 2;
            this.loginButton.Text = "Login";
            this.loginButton.UseVisualStyleBackColor = true;
            this.loginButton.Click += new System.EventHandler(this.loginButton_Click); 

            this.ClientSize = new System.Drawing.Size(310, 70);
            this.Controls.Add(this.loginButton);
            this.Controls.Add(this.pwTextBox);
            this.Controls.Add(this.idTextBox);
            this.Name = "LoginForm";
            this.Text = "Login";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            if (idTextBox.Text == "abcd" && pwTextBox.Text == "abcd")
            {
                MessageBox.Show("Login");
                this.Hide();
                _mainForm.Show();
            }
            else MessageBox.Show("Fail");
        }
    }
}
 
namespace test
{
    static class Program 
    { 
        [STAThread]
        static void Main() // entry point 
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginForm(new Form1()));
        }
    }
}
 
Share this answer
 
v2

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