gelatofu

LabExer5_Login.cs

Mar 29th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.IO;
  5.  
  6. namespace MyForm
  7. {
  8.     public class Login : Form
  9.     {
  10.         private Button btLogin;
  11.         private Button btIndex;
  12.         private TextBox username;
  13.         private TextBox password;
  14.         private Label un;
  15.         private Label pw;
  16.         private CheckBox chPass;
  17.  
  18.  
  19.         public Login()
  20.         {
  21.             InitializeComponents();
  22.         }
  23.  
  24.         public void InitializeComponents()
  25.         {
  26.             this.Text = "Login Form";
  27.             this.BackColor = Color.Lavender;
  28.             this.ForeColor = Color.Black;
  29.             this.Size = new Size(250, 300);
  30.             this.StartPosition = FormStartPosition.CenterScreen;
  31.  
  32.  
  33.             btLogin = new Button();
  34.             btLogin.Text = "Login";
  35.             btLogin.Location = new Point(80, 180);
  36.             btLogin.Click += btLogin_Click;
  37.  
  38.             btIndex = new Button();
  39.             btIndex.Text = "Back";
  40.             btIndex.Location = new Point(80, 210);
  41.             btIndex.Click += btIndex_Click;
  42.  
  43.             un = new Label();
  44.             un.Text = "Username";
  45.             un.Location = new Point(20, 80);
  46.  
  47.             username = new TextBox();
  48.             username.Text = "";
  49.             username.MaxLength = 10;
  50.             username.Location = new Point(80, 80);
  51.  
  52.             pw = new Label();
  53.             pw.Text = "Password";
  54.             pw.Location = new Point(20, 110);
  55.  
  56.             password = new TextBox();
  57.             password.Text = "";
  58.             password.UseSystemPasswordChar = true;
  59.             password.Location = new Point(80, 110);
  60.  
  61.             chPass = new CheckBox();
  62.             chPass.Location = new Point(79, 130);
  63.             chPass.Text = "Show Password";
  64.             chPass.UseVisualStyleBackColor = true;
  65.             chPass.CheckedChanged += new System.EventHandler(chPass_CheckedChanged);
  66.  
  67.             this.Controls.Add(username);
  68.             this.Controls.Add(password);
  69.             this.Controls.Add(btLogin);
  70.             this.Controls.Add(un);
  71.             this.Controls.Add(pw);
  72.             this.Controls.Add(chPass);
  73.             this.Controls.Add(btIndex);
  74.         }
  75.  
  76.         public void btLogin_Click(object sender, EventArgs e)
  77.         {
  78.             string path = String.Concat("", username.Text, ".txt");
  79.             string[] user = new string[2];
  80.  
  81.             if (username.Text == "" || password.Text == "")
  82.             {
  83.                 MessageBox.Show("No username/password. \nPlease input your log-in credentials.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  84.                 return;
  85.             }
  86.  
  87.             if (!File.Exists(path))
  88.             {
  89.                 MessageBox.Show("Please input a valid username.", "Invalid Username", MessageBoxButtons.OK, MessageBoxIcon.Error);
  90.                 return;
  91.             }
  92.             else
  93.             {
  94.                 using (StreamReader sr = new StreamReader(path))
  95.                 {
  96.                     for (int i = 0; i < user.Length; i++)
  97.                     {
  98.                         user.SetValue(sr.ReadLine(), i);
  99.                     }
  100.                 }
  101.                 if ((string)(user.GetValue(0)) != password.Text)
  102.                 {
  103.                     MessageBox.Show("Please input the correct password.", "Incorrect Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
  104.                     return;
  105.                 }
  106.                 else
  107.                 {
  108.                     if ((string)(user.GetValue(1)) == "Type One")
  109.                     {
  110.                         TypeOne one = new TypeOne();
  111.                         this.Hide();
  112.                         one.Show();
  113.                     }
  114.                     else
  115.                     {
  116.                         password.UseSystemPasswordChar = true;
  117.                         TypeTwo two = new TypeTwo();
  118.                         this.Hide();
  119.                         two.Show();
  120.                        
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.  
  126.         public void btIndex_Click(object sender, EventArgs e)
  127.         {
  128.             Index ind = new Index();
  129.             ind.Show();
  130.             this.Hide();
  131.  
  132.         }
  133.  
  134.         private void chPass_CheckedChanged(object sender, EventArgs e)
  135.         {
  136.             if (chPass.CheckState == CheckState.Checked)
  137.                 password.UseSystemPasswordChar = false;
  138.             else
  139.                 password.UseSystemPasswordChar = true;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment