Skip to content

Commit

Permalink
Added PasswordTextBox to FlagConsole project.
Browse files Browse the repository at this point in the history
Added demo of PasswordTextBox to FlagConsole.Demo project.
  • Loading branch information
Kirlu committed Oct 2, 2012
1 parent e8b05bc commit 6a97453
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
12 changes: 10 additions & 2 deletions FlagConsole/FlagConsole.Demo/DemoScreen.cs
Expand Up @@ -26,10 +26,11 @@ public DemoScreen()
};
this.mainMenuPanel.Controls.Add(mainMenuTextLabel);

this.mainMenu = new Menu<Action> { RelativeLocation = new Coordinate(0, 7), Size = new Size(15, 10) };
this.mainMenu = new Menu<Action> { RelativeLocation = new Coordinate(0, 7), Size = new Size(18, 10) };
this.mainMenu.Items.Add(new MenuItem<Action>("Label", this.ShowLabelDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("ListView", this.ShowListViewDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("TextBox", this.ShowTextBoxDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("PasswordTextBox", this.ShowPasswordTextBoxDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("Rectangle", this.ShowRectangleDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("Line", this.ShowLineDemo));
this.mainMenu.Items.Add(new MenuItem<Action>("Ellipse", this.ShowEllipseDemo));
Expand Down Expand Up @@ -60,7 +61,7 @@ private void SwitchDemoPanel(Panel panel)
this.Controls.Remove(this.presentationPanel);
this.presentationPanel = panel;
this.presentationPanel.Size = new Size(45, 40);
this.presentationPanel.RelativeLocation = new Coordinate(35, 7);
this.presentationPanel.RelativeLocation = new Coordinate(33, 7);
this.Controls.Add(this.presentationPanel);
}

Expand All @@ -81,6 +82,13 @@ private void ShowTextBoxDemo()
panel.Activate();
}

private void ShowPasswordTextBoxDemo()
{
var panel = new PasswordTextBoxDemoPanel();
this.SwitchDemoPanel(panel);
panel.Activate();
}

private void ShowRectangleDemo()
{
this.SwitchDemoPanel(new RectangleDemoPanel());
Expand Down
1 change: 1 addition & 0 deletions FlagConsole/FlagConsole.Demo/FlagConsole.Demo.csproj
Expand Up @@ -40,6 +40,7 @@
<Compile Include="LabelDemoPanel.cs" />
<Compile Include="LineDemoPanel.cs" />
<Compile Include="ListViewDemoPanel.cs" />
<Compile Include="PasswordTextBoxDemoPanel.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DemoScreen.cs" />
Expand Down
41 changes: 41 additions & 0 deletions FlagConsole/FlagConsole.Demo/PasswordTextBoxDemoPanel.cs
@@ -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();
}
}
}
42 changes: 42 additions & 0 deletions FlagConsole/FlagConsole/Controls/PasswordTextBox.cs
@@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions FlagConsole/FlagConsole/FlagConsole.csproj
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Controls\MenuEventArgs.cs" />
<Compile Include="Controls\MenuItem.cs" />
<Compile Include="Controls\Panel.cs" />
<Compile Include="Controls\PasswordTextBox.cs" />
<Compile Include="Controls\Screen.cs" />
<Compile Include="Controls\TextBox.cs" />
<Compile Include="Controls\TextField.cs" />
Expand Down

0 comments on commit 6a97453

Please sign in to comment.