Repeatable code was moved to a separate method

This commit is contained in:
Alexander 2019-11-27 16:31:23 +02:00
parent 9435d311c2
commit da954934f9

View file

@ -14,40 +14,38 @@ public ChangeKeyForm()
statusLabel.Text = ""; statusLabel.Text = "";
} }
/*Buttons*/ #region Methods
private async void AcceptButton_Click(object sender, EventArgs e) private async void ChangePasswordStatus(string message, Color color)
{
statusLabel.ForeColor = color;
statusLabel.Text = message;
oldKeyTextBox.Text = "";
newKeyTextBox.Text = "";
await Task.Delay(2500);
statusLabel.Text = "";
}
#endregion
#region Event Handlers
private void AcceptButton_Click(object sender, EventArgs e)
{ {
oldKeyTextBox.Focus(); oldKeyTextBox.Focus();
if (oldKeyTextBox.Text == PublicVar.password.Get() & oldKeyTextBox.Text != newKeyTextBox.Text) if (oldKeyTextBox.Text == PublicVar.password.Get() & oldKeyTextBox.Text != newKeyTextBox.Text)
{ {
PublicVar.password.Set(newKeyTextBox.Text); PublicVar.password.Set(newKeyTextBox.Text);
PublicVar.passwordChanged = true; PublicVar.passwordChanged = true;
statusLabel.ForeColor = Color.Green; ChangePasswordStatus("Password was successfully changed", Color.Green);
statusLabel.Text = "Password was successfully changed";
oldKeyTextBox.Text = "";
newKeyTextBox.Text = "";
await Task.Delay(3000);
statusLabel.Text = "";
} }
else if (oldKeyTextBox.Text != PublicVar.password.Get()) else if (oldKeyTextBox.Text != PublicVar.password.Get())
{ {
SystemSounds.Hand.Play(); SystemSounds.Hand.Play();
statusLabel.ForeColor = Color.Red; ChangePasswordStatus("Invalid old password", Color.Red);
statusLabel.Text = "Invalid old password";
oldKeyTextBox.Text = "";
newKeyTextBox.Text = "";
await Task.Delay(2000);
statusLabel.Text = "";
} }
else if(oldKeyTextBox.Text == newKeyTextBox.Text) else if (oldKeyTextBox.Text == newKeyTextBox.Text)
{ {
SystemSounds.Hand.Play(); SystemSounds.Hand.Play();
statusLabel.ForeColor = Color.Red; ChangePasswordStatus("New password is the same as old", Color.Red);
statusLabel.Text = "New password is the same as old";
oldKeyTextBox.Text = "";
newKeyTextBox.Text = "";
await Task.Delay(2000);
statusLabel.Text = "";
} }
} }
@ -60,10 +58,7 @@ private void PasswordGeneratorButton_Click(object sender, EventArgs e)
passwordGeneratorFrom.Show(this); passwordGeneratorFrom.Show(this);
} }
} }
/*Buttons*/
/*Enter keys area*/
private void ShowOldKeyPictureBox_Click(object sender, EventArgs e) private void ShowOldKeyPictureBox_Click(object sender, EventArgs e)
{ {
if (oldKeyTextBox.UseSystemPasswordChar) if (oldKeyTextBox.UseSystemPasswordChar)
@ -148,7 +143,7 @@ private void OldKeyTextBox_KeyDown(object sender, KeyEventArgs e)
AcceptButton_Click(sender, e); AcceptButton_Click(sender, e);
} }
} }
/*Enter keys area*/ #endregion
} }