using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Common; using System.Diagnostics; namespace PersonalLaundry { public partial class LoginForm : Form { public event EventHandler LoginValidated; public OperatorLogin operatorLogin; ConsoleForm console; public LoginForm(ConsoleForm console) { this.console = console; InitializeComponent(); } private void LoginBox_Load(object sender, EventArgs e) { LoadOperatorLogins(); this.ActiveControl = Username; } private void LoginButton_Click(object sender, EventArgs e) { AttemptLoginOperator(); } private void Password_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Enter) return; this.AttemptLoginOperator(); } private void LoadOperatorLogins() { try { using (var db = new DataModel()) { Username.DataSource = db.OperatorLogins.Where(u => u.OperatorRole.CanLogin == true && u.OperatorRoleId == 3).ToList(); } } catch { } } private void AttemptLoginOperator() { if (Username.Text == null || Password.Text == null) { MessageBox.Show("Please fill in both the username and password fields...", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (var db = new DataModel()) { var login = db.LoginOperator(Username.Text, Password.Text).FirstOrDefault(); if (login == null) { MessageBox.Show("Please fill in a valid username and password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error); Password.Text = string.Empty; return; } operatorLogin = db.OperatorLogins.FirstOrDefault(f => f.OperatorLoginId == login.OperatorLoginId); console.operatorLogin = operatorLogin; if (LoginValidated != null) LoginValidated(this, new EventArgs()); Invoke(new MethodInvoker(Close)); } } } }