Part 11 Bringing It All Together
Replace the code on the AresForm.cs with the following:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Security.Principal;
namespace Ares
{
public partial class AresForm : Form
{
public AresForm()
{
InitializeComponent();
//Make the form go Full Screen
GoFullscreen(true);
}
public void Wipe_Clean()
{
//Reset all of the variables ready for the next User.
Globals.siteVar = “”;
Globals.accountname = “”;
Globals.adGroup = “”;
Globals.foreName = “”;
Globals.surName = “”;
Globals.staStuVol = “”;
Globals.homeDir = “”;
Globals.profileDir = “”;
Globals.cnValue = “”;
Globals.exchangeDB = “”;
Globals.ExchPolicy = “”;
Globals.miFare = “”;
CardtextBox.Focus();
}
public void PrintReceipt(string textToSend)
{
//Create an instance of our printer class
PCPrint printer = new PCPrint();
//Set the font we want to use
printer.PrinterFont = new Font(“Verdana”, 10);
//Set the TextToPrint property
printer.TextToPrint = textToSend;
//Issue print command
printer.PrinterSettings.PrinterName = @”<Printer Name of Till Receipt Printer>”;
using (WindowsImpersonationContext wic = WindowsIdentity.Impersonate(IntPtr.Zero))
{
//code to send printdocument to the printer
printer.Print();
}
}
private void GoFullscreen(bool fullscreen)
{
if (fullscreen)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
}
else
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
private async void CardtextBox_TextChanged(object sender, EventArgs e)
{
//Scanning a user card triggers the process of changing a password or creating an account
//Retrieve the contents of the text box (which is hidden behind the Logo)
//Only act when over seven characters have been read
if (CardtextBox.Text.Length > 7)
{
#region variables
String CardReaderValue = CardtextBox.Text;
string activeUser = “No”;
string adAccount = “No”;
string newPassword = “Rand0mPa55word”;
string printText = “”;
#endregion
//Generate a new password
ChangePassword passWordChange = new ChangePassword();
try
{
#region check if a valid user
//Check if student is active
CheckIfActive checkif = new CheckIfActive();
activeUser = checkif.IsActive(CardReaderValue);
if (activeUser != null)
{
switch (activeUser)
{
case “No”:
#region not active student
//Show message that this card has not exist on the system
CatchError.Text = “This card does not belong to a valid user.”;
break;
#endregion
case “Yes”:
#region is an active user
//Check if Active Directory account exists. Change password if yes, otherwise create the account
CheckIfExists checkad = new CheckIfExists();
adAccount = checkad.ADExists(Globals.accountname);
switch (adAccount)
{
case “No”:
#region no account
//No existing account. Create an account for the user.
CreateAccount newAccount = new CreateAccount();
//Grab all of the variables from static (Globals) to local temp variables to avoid contents dropping mid program
string tempAccountname = Globals.accountname;
string tempForename = Globals.foreName;
newAccount.AccountCreation(tempAccountname);
newPassword = passWordChange.PassWordReset(tempAccountname);
System.Threading.Thread.Sleep(5000); //To give AD 5 seconds to catch up before creating the mailbox
//Enable mailbox
Enable_Mailbox enableTheMailbox = new Enable_Mailbox();
enableTheMailbox.EnableMailbox(tempAccountname);
//Print the receipt containing the account details
printText = ” Welcome ” + tempForename + “\r\n\nYour Details are\r\nUsername: ” + tempAccountname + “\r\nPassword: ” + newPassword + “\r\nEmail: ” + tempAccountname + “@<rest of user email address>\r\n\n” + “You will need to change your password the first time you log in.\r\n\n<Add any message that you want to display here>\r\n\nPrinted: ” + DateTime.Now;
PrintReceipt(printText);
CatchError.ForeColor = Color.DarkGreen;
CatchError.Text = “Please take the receipt with your account details.”;
break;
#endregion
case “Yes”:
#region account present
//Account exists. Change user’s password.
newPassword = passWordChange.PassWordReset(Globals.accountname);
//Print the receipt containing the new password
printText = ” Welcome ” + Globals.foreName + “\r\n\nYour Details are\r\nUsername: ” + Globals.accountname + “\r\nPassword: ” + newPassword + “\r\nEmail: ” + Globals.accountname + “@<rest of user email address>\r\n\n” + “You will need to change your password the first time you log in.\r\n\n<Add any message that you want to display here>\r\n\nPrinted: ” + DateTime.Now;
PrintReceipt(printText);
CatchError.ForeColor = Color.DarkGreen;
CatchError.Text = “Password changed. Please take your receipt.”;
break;
#endregion
default:
#region something went wrong
CatchError.ForeColor = Color.Red;
CatchError.Text = “Unable to determine if an your account already exists.”;
break;
#endregion
}
break;
#endregion
default:
CatchError.Text = activeUser + ” : ” + Globals.errorCode;
break;
}
await WaitMethod();
Wipe_Clean();
CardtextBox.Focus();
}
else
{
await WaitMethod();
Wipe_Clean();
}
CatchError.ForeColor = Color.Black;
CatchError.Text = “Please touch your Card to the reader.”;
}
catch (Exception etest)
{
CatchError.Text = etest.ToString() + ” : ” + Globals.errorCode;
await WaitMethod();
Wipe_Clean();
CardtextBox.Focus();
CatchError.ForeColor = Color.Black;
CatchError.Text = “Please touch your Card to the reader.”;
}
#endregion
CardtextBox.Text = “”;
}
}
private void AcreForm_Load(object sender, EventArgs e)
{
Wipe_Clean();
CardtextBox.Focus();
}
async System.Threading.Tasks.Task WaitMethod()
{
await System.Threading.Tasks.Task.Delay(15000);
}
async System.Threading.Tasks.Task WaitMethodShort()
{
await System.Threading.Tasks.Task.Delay(5000);
}
}
}