Login
How to create Login Form and give privileges to users to open particular forms only in c#.net Windows form..?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Feb 26, 2014, 4:49 AM
I have attached a simple sample for this login logic for Windows Forms you have asked by using the following code snippet,
Here you can select the user name from a combobox and the password is "tamil_valarga".
Form 1:
namespace Login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox2.Text == "tamil_valarga")
{
if (this.comboBox1.Text == "User 1")
{
new User_1().Show();
}
else if (this.comboBox1.Text == "User 2")
{
new User_2().Show();
}
else if (this.comboBox1.Text == "Admin")
{
new Admin().Show();
}
else
{
MessageBox.Show("User Name is wrong or not have permission to access this application");
}
}
else
{
MessageBox.Show("Password you have entered is wrong");
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
private void InitializeComponent()
{
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(113, 83);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = '*';
this.textBox2.Size = new System.Drawing.Size(155, 20);
this.textBox2.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(33, 38);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 13);
this.label1.TabIndex = 2;
this.label1.Text = "User Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(33, 83);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(59, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Pass Word";
//
// button1
//
this.button1.Location = new System.Drawing.Point(63, 129);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 26);
this.button1.TabIndex = 4;
this.button1.Text = "Enter";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(156, 129);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(79, 26);
this.button2.TabIndex = 5;
this.button2.Text = "Exit";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"User 1",
"User 2",
"Administrator"});
this.comboBox1.Location = new System.Drawing.Point(112, 38);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(155, 21);
this.comboBox1.TabIndex = 6;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(303, 181);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
Regards,
Selva Ganapathy K