Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have a form as login form for enter ID and Password.
if user enter correct id/pass,i want to close the login form and open the main form.
how to do that?
i wrote this code but both forms closed!!

C#
if(//check ID/pass in DB......)
{
this.close();
form2 frm=new form2();
frm.showDialog();
}
please help,Best Regards
Posted
Updated 25-Mar-17 16:06pm
Comments
Perić Željko 28-Sep-12 7:21am    
You need to call LogIn Form from Main Form. When user enters password Log In Form automatically closes and Main Form continue running.
Look at solution no. 5

Hi,

try this code:
if(//check ID/pass in DB......)
{
this.Hide();
form2 frm=new form2();
frm.ShowDialog();
this.Show();
}
 
Share this answer
 
problem is in closing statement of login form.
My advice is to call login form from another place (Main method for instance) and if login is successful only then you should call required form.
Take this code as example:
C#
//Application's entry point
[STAThread]
Main(string[]args)
{
    LoginForm loginFrm = new LoginForm();
    //if login successful proceed
    if(loginFrm.ShowDialog() == DialogResult.OK)
    {
        AnotherForm frm = new AnotherForm();
        frm.Show();
    }
    //if not, sorry no form. 
}
 
Share this answer
 
if(//check ID/pass in DB......)
{
this.close();
form2 frm=new form2();
frm.Show();
}
 
Share this answer
 
C#
if(Check user & pass)
{
    this.Hide();
    new MainForm().ShowDialog();
}
 
Share this answer
 
Comments
FM7 28-Sep-12 6:58am    
i want to close form not hide.
problem is:form1 is parent and form2 is child.
how to change parent and child forms to eah other?
Hello,
this is simple win form application written in ide sharp develop , C# .Net 4.o.

it consists of Main Form in which are these controls :
label1 , containing "Entered Access Code" message
label2 , containing "Access Granted" or "Access Denied" message
textBox1, renamed to "password" and set to read only
button1, with text property set to "ok"

and LogInForm in wich are these controls :
textbox1 , renamed to "password"
button1 , with text property set to "continue"

When user starts program, first is shown Log In Form Dialog, waiting for user to enter password. When "password" is entered the Log In Form Dialog closes and Main Form shows and becomes active form of application.
If entered password is not valid the label2 changes to "Access Denied" message,
or if entered password is correct the label2 changes to "Access Granted" message, and button1 text property changes to "continue" or
"try again".
When user "try again" to enter password Log In Dialog shows again.
The main form is still visible but not active and not accessable while user enters
new password or closes Log In Dialog.
Here is the whole open source code needed for Application. Notice the .Net version 4.0.

MainForm.cs*

C#
/*
 * Created by SharpDevelop.c# , .Net Framework 4.0
 * User: Perić Željko
 * Date: 28.9.2012
 * Time: 11:53
 *
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Log_In_Example
{
    /// <summary>
    /// Description of MainForm.
    /// Show Log In Dialog Form and check password
    /// entered by user.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // call ShowLogInDialog()
            //
            ShowLogInDialog();
        }

        void ShowLogInDialog()
        {
            //
            // show Log In Dialog
            //

            // correct password
            const string correct_ID = "1234567890";
            //
            LogInForm LogCheck = new LogInForm();
            //
            // ShowDialog() is used instead of Show()
            // because this way dialog stays active on top of all others
            // forms and only accessible Form of this application.
            //
            LogCheck.ShowDialog();
            //
            // set value of password.Text (textBox1) of Main Form,
            // to entered value in LogInForm.
            //
            password.Text = LogCheck.password.Text;
            if (correct_ID == password.Text)
            {
                label2.Text = "Access Granted";
                button1.Text ="continue";
            }
            else
            {
                label2.Text = "Access Denied";
                button1.Text ="try again";
            }
            //
            LogCheck.Dispose();
            Refresh();
        }
        void Button1Click(object sender, EventArgs e)
        {
            //
            // If Entered "password" was ok close Main Form
            // else try again to enter password
            // here you can count how many times user made error
            // and prevent another try.
            // Wach out where You are going to declare variable for that.
            //
            if (button1.Text == "continue")
            {
                Close();
            }
            else if (button1.Text == "try again")
            {
                ShowLogInDialog();
            }
        }
    }
}


MainForm.Designer.cs

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 28.9.2012
 * Time: 11:53
 * 
 */
namespace Log_In_Example
{
	partial class MainForm
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.password = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// password
			// 
			this.password.Location = new System.Drawing.Point(11, 39);
			this.password.MaxLength = 10;
			this.password.Name = "password";
			this.password.ReadOnly = true;
			this.password.Size = new System.Drawing.Size(268, 31);
			this.password.TabIndex = 10;
			this.password.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Georgia", 15.75F, ((System.Drawing.FontStyle)(((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic) 
												| System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
			this.label1.Location = new System.Drawing.Point(12, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(267, 27);
			this.label1.TabIndex = 1;
			this.label1.Text = "Entered Access Code";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(12, 133);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(267, 37);
			this.button1.TabIndex = 2;
			this.button1.Text = "ok";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(12, 93);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(267, 27);
			this.label2.TabIndex = 3;
			this.label2.Text = "Access Code";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// MainForm
			// 
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
			this.ClientSize = new System.Drawing.Size(292, 175);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.password);
			this.Font = new System.Drawing.Font("Georgia", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(204)));
			this.MaximizeBox = false;
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = " Log In Example";
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox password;
	}
}



LogInForm.cs

C#
/*
 * Created by SharpDevelop. C#, ,Net Framework 4.0
 * User: Perić Željko
 * Date: 28.9.2012
 * Time: 11:54
 *
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Log_In_Example
{
    /// <summary>
    /// Dialog for accepting Log ID from user
    /// </summary>
    public partial class LogInForm : Form
    {
        public LogInForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

        }

        void Button1Click(object sender, EventArgs e)
        {
            //
            // Close Log In Form
            //
            Close();
        }

        void PasswordKeyPress(object sender, KeyPressEventArgs e)
        {
            //
            // If user press "Enter" inside
            // password text box LogIn Form Closes
            //
            if (e.KeyChar == (char) Keys.Enter)
            {
                Close();
            }
        }
    }
}


LogInForm.Designer.cs*

/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 28.9.2012
 * Time: 11:54
 * 
 */
namespace Log_In_Example
{
	partial class LogInForm
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.password = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(9, 49);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(271, 32);
			this.button1.TabIndex = 1;
			this.button1.Text = "continue";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// password
			// 
			this.password.Location = new System.Drawing.Point(9, 12);
			this.password.MaxLength = 10;
			this.password.Name = "password";
			this.password.Size = new System.Drawing.Size(271, 26);
			this.password.TabIndex = 0;
			this.password.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.password.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.PasswordKeyPress);
			// 
			// LogInForm
			// 
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
			this.ClientSize = new System.Drawing.Size(292, 94);
			this.ControlBox = false;
			this.Controls.Add(this.password);
			this.Controls.Add(this.button1);
			this.Font = new System.Drawing.Font("Georgia", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "LogInForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "    Log In";
			this.TopMost = true;
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		public System.Windows.Forms.TextBox password;
		private System.Windows.Forms.Button button1;
	}
}



You can easily modify the program to count how many try's where to enter password.
 
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