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*
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Log_In_Example
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
ShowLogInDialog();
}
void ShowLogInDialog()
{
const string correct_ID = "1234567890";
LogInForm LogCheck = new LogInForm();
LogCheck.ShowDialog();
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 (button1.Text == "continue")
{
Close();
}
else if (button1.Text == "try again")
{
ShowLogInDialog();
}
}
}
}
MainForm.Designer.cs
namespace Log_In_Example
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
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();
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;
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;
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);
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;
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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Log_In_Example
{
public partial class LogInForm : Form
{
public LogInForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
Close();
}
void PasswordKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char) Keys.Enter)
{
Close();
}
}
}
}
LogInForm.Designer.cs*
namespace Log_In_Example
{
partial class LogInForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.password = new System.Windows.Forms.TextBox();
this.SuspendLayout();
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);
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);
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.