-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Kirlu/master
PasswordTextBox
- Loading branch information
Showing
5 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using FlagConsole.Controls; | ||
using FlagConsole.Drawing; | ||
|
||
namespace FlagConsole.Demo | ||
{ | ||
public class PasswordTextBoxDemoPanel : Panel | ||
{ | ||
private readonly Label _descriptionLabel; | ||
private readonly Label _textLabel; | ||
private readonly PasswordTextBox _passwordTextBox; | ||
|
||
public PasswordTextBoxDemoPanel() | ||
{ | ||
_descriptionLabel = new Label | ||
{ Text = "Enter a password and press enter. The maximum length is set to 5 characters, but it has a width of 8. Of course, this limits can be increased." }; | ||
_descriptionLabel.Size = new Size(_descriptionLabel.Text.Length/3 + 1, 4); | ||
Controls.Add(_descriptionLabel); | ||
|
||
_passwordTextBox = new PasswordTextBox { Size = new Size(8, 1), MaxLength = 5, RelativeLocation = new Coordinate(0, 4) }; | ||
_passwordTextBox.TextSubmitted += PasswordTextBoxPasswordSubmitted; | ||
Controls.Add(_passwordTextBox); | ||
|
||
_textLabel = new Label { RelativeLocation = new Coordinate(0, 6) }; | ||
Controls.Add(_textLabel); | ||
} | ||
|
||
private void PasswordTextBoxPasswordSubmitted(object sender, EventArgs e) | ||
{ | ||
_textLabel.Text = "You have entered: " + _passwordTextBox.Password; | ||
_textLabel.Size = new Size(_textLabel.Text.Length, 1); | ||
|
||
OnInvalidated(EventArgs.Empty); | ||
} | ||
|
||
public void Activate() | ||
{ | ||
_passwordTextBox.Focus(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
|
||
namespace FlagConsole.Controls | ||
{ | ||
/// <summary> | ||
/// Provides a TextBox control, where the user can type passwords. | ||
/// </summary> | ||
public class PasswordTextBox : TextBox | ||
{ | ||
public PasswordTextBox() | ||
{ | ||
Password = string.Empty; | ||
TextChanged += OnTextChanged; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current password of the <see cref="PasswordTextBox"/>. | ||
/// </summary> | ||
/// <value> | ||
/// The current password of the <see cref="PasswordTextBox"/>. | ||
/// </value> | ||
public string Password { get; private set; } | ||
|
||
/// <summary> | ||
/// Replace the entered character in the <see cref="PasswordTextBox"/> if any with * and save in Password property. | ||
/// If character is deleted from the <see cref="PasswordTextBox"/> the character is deleted from the Password property. | ||
/// </summary> | ||
private void OnTextChanged(object sender, EventArgs eventArgs) | ||
{ | ||
if(Text.Length > Password.Length) | ||
{ | ||
var lastChar = Text[Text.Length - 1]; | ||
Password += lastChar; | ||
Text = Text.Replace(lastChar, '*'); | ||
} | ||
else if(Text.Length < Password.Length) | ||
{ | ||
Password = Password.Remove(Password.Length - 1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters