📄 gui.cs
字号:
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.plaintext.Enabled = true;
this.ciphertext.Enabled = false;
this.plaintext.Focus();
this.encryptbutton.Enabled = true;
this.decryptbutton.Enabled = false;
}
private void plainButton_CheckedChanged(object sender, System.EventArgs e)
{
this.ciphertext.Enabled = false; //disabled field
this.encryptbutton.Enabled = true; //enabled field
this.decryptbutton.Enabled = false; //disabled field
this.plaintext.Enabled = true; //disabled field
//changes text in accordance with encryption method selection
this.label1.Text = "Please enter the value to encrypt by:";
this.plaintext.Focus(); //set focus
this.IncValue.Clear(); //clear the value
}
private void cipherButton_CheckedChanged(object sender, System.EventArgs e)
{
this.plaintext.Enabled = false; //disabled field
this.ciphertext.Enabled = true; //enabled field
this.encryptbutton.Enabled = false; //disabled field
this.decryptbutton.Enabled = true; //enabled field
//changes text in accordance with decryption method selection
this.label1.Text = "Please enter the value to decrypt by:";
this.ciphertext.Focus(); //set the focus
this.IncValue.Clear(); //clear the field
}
private void decryptbutton_Click(object sender, System.EventArgs e)
{
//the following code opens up the file ErrorLog.txt and writes the error
//details to this file for further information
StreamWriter sr = File.AppendText(FILE_NAME);
Decryption decrypt = new Decryption();// create new instance of class Decryption
Unicode unicode = new Unicode(); // create new insatnce of class Unicode
string temp = "";
if(this.ciphertext.Text == "") //if no text entered give error message
{
MessageBox.Show("Cannot Decrypt - no cyphertext entered!");
this.ciphertext.Focus(); //set key focus to ciphertext field
}
else
try //try block for the decryption fucntion
{//pass in the text and increment value and return temp
temp = decrypt.Decrypt(this.ciphertext.Text, this.IncValue.Text);
this.plaintext.Text = temp; //display text in temp
}
//Catch Method 1: catch the Format and OverflowException in the one catch
//and sort into types
catch(Exception excep)//catch all exceptions
{
//get the type of exception and converts it to a string
string type = excep.GetType().ToString();
string desc = excep.ToString();
//if the num entered is not a number or nothing is entered
if(type == "System.FormatException")
{
MessageBox.Show("Please Enter in a value by which to Decrypt by", "Error");
this.IncValue.Focus();//set focus to IncValue field
this.IncValue.Clear();//clear the field
}
//if too many integers are entered into the field
else if(type == "System.OverflowException")
{
MessageBox.Show("Number to large to encrypt, please enter in another number", "Error");
this.IncValue.Focus();//set focus to IncValue field
this.IncValue.Clear();//clear the field
}
else
{
MessageBox.Show("Unexpected Exception - See ErrorLog.txt for further information" ,"Error");
}
//write all the error details to a log file called errorlog.txt,
// user can read the file and view descriptions
sr.WriteLine("The following is a detailed description of the error\n" + desc);
}
sr.Close(); // close the file
}
private void encryptbutton_Click(object sender, System.EventArgs e)
{
//opens up the file ErrorLog.txt and writes the error
//details to this file for further information
StreamWriter sr = File.AppendText(FILE_NAME);
Encryption encrypt = new Encryption(); // create new instance of class Encryption
Unicode unicode = new Unicode(); // create new instance of class Encryption
string temp = ""; // initialise temp string to null
if(this.plaintext.Text == "")
{
MessageBox.Show("Cannot Encrypt - No Plaintext entered");
this.plaintext.Focus(); //set cursor to plaintext
}
else
try//try block for the decryption fucntion
{//pass in the text and increment value and return temp
temp = encrypt.Encrypt(this.plaintext.Text, this.IncValue.Text);
ciphertext.Text = temp; //display text in ciphertext field
}
//Catch Method 2 catch the Format and OverflowException seperately
catch(FormatException fexcept)
{
MessageBox.Show("Please enter in a numeric value by which to Encrypt by", "Error");
string tempex = fexcept.ToString(); //sets tempex to string of fexecpt variable
this.IncValue.Clear(); //clears the field
this.IncValue.Focus(); //sets focus on the field
//The following line writes out the error to the text file ErrorLog.txt
sr.WriteLine("The following is a detailed description of the error\n" + tempex);
}
catch(OverflowException overex)
{
MessageBox.Show("Number to large to encrypt, please enter in another number", "Error");
string tempex = overex.ToString(); //sets tempex to string of overex variable
this.IncValue.Clear(); //clears field
this.IncValue.Focus(); //sets focus
//print out error file
sr.WriteLine("The following is a detailed description of the error\n" + tempex);
}
sr.Close();//close the file
}
//The following function performs as a key listener and only permits numeric entry
private void IncValue_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs ke)
{
bool flag;
//call to function isHandled to determine whether the key pressed is valid or not
flag = isHandled(this.IncValue.Text,ke);
//gets return value and if true then handle the event
//i.e do not let text be inputted into the text area
//if false permit
if(flag == true)
{
ke.Handled = true;
}
}
private bool isHandled(object sender, System.Windows.Forms.KeyPressEventArgs ke)
{
int numkey = 0;
numkey = (int)ke.KeyChar;//gets the character corresponding to the key pressed
bool flag;
//if the character is less than 48 or greater than 57 or not a backspace in ascii
if((numkey < 48 || numkey > 57) && (numkey != 8))
//(48ascii = 0 in dec and 57ascii = 9 in dec)
{
flag = true; //return true digits 0 - 9, backspace
return flag; // return the flag
}
else
{
flag = false; // else set the flag to false
return flag; //return the flag
}
}
private void Clear_Click(object sender, System.EventArgs e)
{
this.plaintext.Clear(); //clear the plaintext field
this.ciphertext.Clear(); //clear the ciphertext field
this.IncValue.Clear(); //clear the value field
}
private void Quit_Click(object sender, System.EventArgs e)
{
Application.Exit();//exit the application
}
//following code displays text after the mouse has been placed over the plaintext textfield
private void plaintext_MouseHover(object sender, System.EventArgs e)
{
toolTip1.SetToolTip(this.plaintext, "Enter your plaintext \nto encrypt here");
}
//following code displays text after the mouse has been placed over the ciphertext textfield
private void ciphertext_MouseHover(object sender, System.EventArgs e)
{
toolTip2.SetToolTip(this.ciphertext, "Enter your ciphertext \nto decrypt here");
}
//following code displays text after the mouse has been placed over the encrypt button
private void encryptbutton_MouseHover(object sender, System.EventArgs e)
{
toolTip3.SetToolTip(this.encryptbutton, "Encrypts plaintext to cyphertext");
}
//following code displays text after the mouse has been placed over the decrypt button
private void decryptbutton_MouseHover(object sender, System.EventArgs e)
{
toolTip4.SetToolTip(this.decryptbutton, "Decrypts from cyphertext to plaintext");
}
//following code displays text after the mouse has been placed over the clear button
private void Clear_MouseHover(object sender, System.EventArgs e)
{
toolTip5.SetToolTip(this.Clear, "Clears all text fields");
}
//following code displays text after the mouse has been placed over the plaintext textfield
private void Quit_MouseHover(object sender, System.EventArgs e)
{
toolTip6.SetToolTip(this.Quit, "Quits the application");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -