Added new TextBox control with placeholder, instead of realization via labels

This commit is contained in:
Alexander 2019-10-28 20:32:13 +02:00
parent 41bc4a75cd
commit f98b4bb732
8 changed files with 296 additions and 111 deletions

View file

@ -28,12 +28,10 @@ protected override void Dispose(bool disposing)
/// </summary>
private void InitializeComponent()
{
this.oldKeyTextBox = new System.Windows.Forms.TextBox();
this.newKeyTextBox = new System.Windows.Forms.TextBox();
this.oldKeyTextBox = new System.Windows.Forms.PlaceholderTextBox();
this.newKeyTextBox = new System.Windows.Forms.PlaceholderTextBox();
this.acceptButton = new System.Windows.Forms.Button();
this.mainPanel = new System.Windows.Forms.Panel();
this.oldKeyPlaceholder = new System.Windows.Forms.Label();
this.newKeyPlaceholder = new System.Windows.Forms.Label();
this.lockPictureBox = new System.Windows.Forms.PictureBox();
this.showNewKeyPictureBox = new System.Windows.Forms.PictureBox();
this.showOldKeyPictureBox = new System.Windows.Forms.PictureBox();
@ -51,6 +49,10 @@ private void InitializeComponent()
this.oldKeyTextBox.Size = new System.Drawing.Size(177, 22);
this.oldKeyTextBox.TabIndex = 0;
this.oldKeyTextBox.UseSystemPasswordChar = true;
this.oldKeyTextBox.Placeholder = "Old key";
this.oldKeyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray;
this.oldKeyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F);
this.oldKeyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray;
this.oldKeyTextBox.TextChanged += new System.EventHandler(this.OldKeyTextBox_TextChanged);
this.oldKeyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OldKeyTextBox_KeyDown);
//
@ -61,9 +63,12 @@ private void InitializeComponent()
this.newKeyTextBox.Size = new System.Drawing.Size(177, 22);
this.newKeyTextBox.TabIndex = 1;
this.newKeyTextBox.UseSystemPasswordChar = true;
this.newKeyTextBox.Placeholder = "New key";
this.newKeyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray;
this.newKeyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F);
this.newKeyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray;
this.newKeyTextBox.TextChanged += new System.EventHandler(this.NewKeyTextBox_TextChanged);
this.newKeyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.NewKeyTextBox_KeyDown);
this.newKeyTextBox.Leave += new System.EventHandler(this.NewKeyTextBox_Leave);
//
// acceptButton
//
@ -79,8 +84,6 @@ private void InitializeComponent()
// mainPanel
//
this.mainPanel.BackColor = System.Drawing.Color.White;
this.mainPanel.Controls.Add(this.oldKeyPlaceholder);
this.mainPanel.Controls.Add(this.newKeyPlaceholder);
this.mainPanel.Controls.Add(this.lockPictureBox);
this.mainPanel.Controls.Add(this.showNewKeyPictureBox);
this.mainPanel.Controls.Add(this.showOldKeyPictureBox);
@ -92,32 +95,6 @@ private void InitializeComponent()
this.mainPanel.Size = new System.Drawing.Size(269, 75);
this.mainPanel.TabIndex = 6;
//
// oldKeyPlaceholder
//
this.oldKeyPlaceholder.AutoSize = true;
this.oldKeyPlaceholder.Cursor = System.Windows.Forms.Cursors.IBeam;
this.oldKeyPlaceholder.Enabled = false;
this.oldKeyPlaceholder.ForeColor = System.Drawing.Color.DarkGray;
this.oldKeyPlaceholder.Location = new System.Drawing.Point(67, 16);
this.oldKeyPlaceholder.Name = "oldKeyPlaceholder";
this.oldKeyPlaceholder.Size = new System.Drawing.Size(46, 13);
this.oldKeyPlaceholder.TabIndex = 8;
this.oldKeyPlaceholder.Text = "Old key";
this.oldKeyPlaceholder.Click += new System.EventHandler(this.OldKeyPlaceholder_Click);
//
// newKeyPlaceholder
//
this.newKeyPlaceholder.AutoSize = true;
this.newKeyPlaceholder.Cursor = System.Windows.Forms.Cursors.IBeam;
this.newKeyPlaceholder.Enabled = false;
this.newKeyPlaceholder.ForeColor = System.Drawing.Color.DarkGray;
this.newKeyPlaceholder.Location = new System.Drawing.Point(67, 41);
this.newKeyPlaceholder.Name = "newKeyPlaceholder";
this.newKeyPlaceholder.Size = new System.Drawing.Size(50, 13);
this.newKeyPlaceholder.TabIndex = 2;
this.newKeyPlaceholder.Text = "New key";
this.newKeyPlaceholder.Click += new System.EventHandler(this.NewKeyPlaceholder_Click);
//
// lockPictureBox
//
this.lockPictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
@ -194,15 +171,13 @@ private void InitializeComponent()
#endregion
private System.Windows.Forms.TextBox oldKeyTextBox;
private System.Windows.Forms.TextBox newKeyTextBox;
private System.Windows.Forms.PlaceholderTextBox oldKeyTextBox;
private System.Windows.Forms.PlaceholderTextBox newKeyTextBox;
private System.Windows.Forms.Button acceptButton;
private System.Windows.Forms.PictureBox showOldKeyPictureBox;
private System.Windows.Forms.PictureBox showNewKeyPictureBox;
private System.Windows.Forms.Panel mainPanel;
private System.Windows.Forms.PictureBox lockPictureBox;
private System.Windows.Forms.Label statusLabel;
private System.Windows.Forms.Label newKeyPlaceholder;
private System.Windows.Forms.Label oldKeyPlaceholder;
}
}

View file

@ -91,15 +91,7 @@ private void OldKeyTextBox_TextChanged(object sender, EventArgs e)
else
{
acceptButton.Enabled = false;
}
if (oldKeyTextBox.Text.Length > 0)
{
oldKeyPlaceholder.Visible = false;
}
else
{
oldKeyPlaceholder.Visible = true;
}
}
}
private void NewKeyTextBox_TextChanged(object sender, EventArgs e)
@ -111,15 +103,7 @@ private void NewKeyTextBox_TextChanged(object sender, EventArgs e)
else
{
acceptButton.Enabled = false;
}
if (newKeyTextBox.Text.Length > 0)
{
newKeyPlaceholder.Visible = false;
}
else
{
newKeyPlaceholder.Visible = true;
}
}
}
private void NewKeyPlaceholder_Click(object sender, EventArgs e)
@ -130,13 +114,6 @@ private void OldKeyPlaceholder_Click(object sender, EventArgs e)
{
oldKeyTextBox.Focus();
}
private void NewKeyTextBox_Leave(object sender, EventArgs e)
{
if (newKeyTextBox.Text.Length == 0)
{
newKeyPlaceholder.Visible = true;
}
}
private void NewKeyTextBox_KeyDown(object sender, KeyEventArgs e)
{

View file

@ -134,6 +134,9 @@
<Compile Include="SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="PlaceholderTextBox.cs">
<SubType>Component</SubType>
</Compile>
<EmbeddedResource Include="AboutFrom.resx">
<DependentUpon>AboutFrom.cs</DependentUpon>
</EmbeddedResource>

View file

@ -29,28 +29,17 @@ protected override void Dispose(bool disposing)
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EnterKeyForm));
this.keyTextBox = new System.Windows.Forms.TextBox();
this.okButton = new System.Windows.Forms.Button();
this.mainPanel = new System.Windows.Forms.Panel();
this.encryptionKeyPlaceholder = new System.Windows.Forms.Label();
this.lockPictureBox = new System.Windows.Forms.PictureBox();
this.fileNameLabel = new System.Windows.Forms.Label();
this.showKeyPictureBox = new System.Windows.Forms.PictureBox();
this.keyTextBox = new System.Windows.Forms.PlaceholderTextBox();
this.mainPanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.lockPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.showKeyPictureBox)).BeginInit();
this.SuspendLayout();
//
// keyTextBox
//
this.keyTextBox.Location = new System.Drawing.Point(64, 37);
this.keyTextBox.Name = "keyTextBox";
this.keyTextBox.Size = new System.Drawing.Size(177, 22);
this.keyTextBox.TabIndex = 0;
this.keyTextBox.UseSystemPasswordChar = true;
this.keyTextBox.TextChanged += new System.EventHandler(this.KeyTextBox_TextChanged);
this.keyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyTextBox_KeyDown);
//
// okButton
//
this.okButton.Enabled = false;
@ -65,7 +54,6 @@ private void InitializeComponent()
// mainPanel
//
this.mainPanel.BackColor = System.Drawing.Color.White;
this.mainPanel.Controls.Add(this.encryptionKeyPlaceholder);
this.mainPanel.Controls.Add(this.lockPictureBox);
this.mainPanel.Controls.Add(this.fileNameLabel);
this.mainPanel.Controls.Add(this.keyTextBox);
@ -76,18 +64,6 @@ private void InitializeComponent()
this.mainPanel.Size = new System.Drawing.Size(269, 75);
this.mainPanel.TabIndex = 5;
//
// encryptionKeyPlaceholder
//
this.encryptionKeyPlaceholder.AutoSize = true;
this.encryptionKeyPlaceholder.Cursor = System.Windows.Forms.Cursors.IBeam;
this.encryptionKeyPlaceholder.Enabled = false;
this.encryptionKeyPlaceholder.ForeColor = System.Drawing.Color.DarkGray;
this.encryptionKeyPlaceholder.Location = new System.Drawing.Point(68, 40);
this.encryptionKeyPlaceholder.Name = "encryptionKeyPlaceholder";
this.encryptionKeyPlaceholder.Size = new System.Drawing.Size(82, 13);
this.encryptionKeyPlaceholder.TabIndex = 9;
this.encryptionKeyPlaceholder.Text = "Encryption key";
//
// lockPictureBox
//
this.lockPictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
@ -123,6 +99,20 @@ private void InitializeComponent()
this.showKeyPictureBox.TabStop = false;
this.showKeyPictureBox.Click += new System.EventHandler(this.ShowKeyPictureBox_Click);
//
// keyTextBox
//
this.keyTextBox.Location = new System.Drawing.Point(64, 37);
this.keyTextBox.Name = "keyTextBox";
this.keyTextBox.Size = new System.Drawing.Size(177, 22);
this.keyTextBox.TabIndex = 0;
this.keyTextBox.UseSystemPasswordChar = true;
this.keyTextBox.Placeholder = "Encryption key";
this.keyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray;
this.keyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F);
this.keyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray;
this.keyTextBox.TextChanged += new System.EventHandler(this.KeyTextBox_TextChanged);
this.keyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyTextBox_KeyDown);
//
// EnterKeyForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -155,7 +145,6 @@ private void InitializeComponent()
private System.Windows.Forms.Panel mainPanel;
private System.Windows.Forms.Label fileNameLabel;
private System.Windows.Forms.PictureBox lockPictureBox;
public System.Windows.Forms.TextBox keyTextBox;
private System.Windows.Forms.Label encryptionKeyPlaceholder;
public System.Windows.Forms.PlaceholderTextBox keyTextBox;
}
}

View file

@ -43,12 +43,10 @@ private void KeyTextBox_TextChanged(object sender, EventArgs e)
if (keyTextBox.Text.Length > 0)
{
okButton.Enabled = true;
encryptionKeyPlaceholder.Visible = false;
}
else
{
okButton.Enabled = false;
encryptionKeyPlaceholder.Visible = true;
}
}

View file

@ -115,10 +115,9 @@ public void InitializeComponent()
this.showTrayMenu = new System.Windows.Forms.ToolStripMenuItem();
this.exitTrayMenu = new System.Windows.Forms.ToolStripMenuItem();
this.fileLockedPanel = new System.Windows.Forms.Panel();
this.encryptionKeyPlaceholder = new System.Windows.Forms.Label();
this.fileLockedCloseButton = new System.Windows.Forms.Button();
this.fileLockedOkButton = new System.Windows.Forms.Button();
this.fileLockedKeyTextBox = new System.Windows.Forms.TextBox();
this.fileLockedKeyTextBox = new System.Windows.Forms.PlaceholderTextBox();
this.fileLockedShowKey = new System.Windows.Forms.PictureBox();
this.fileLockedLabel = new System.Windows.Forms.Label();
this.toolTip = new System.Windows.Forms.ToolTip(this.components);
@ -1058,7 +1057,6 @@ public void InitializeComponent()
this.fileLockedPanel.Anchor = System.Windows.Forms.AnchorStyles.None;
this.fileLockedPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(55)))), ((int)(((byte)(55)))), ((int)(((byte)(55)))));
this.fileLockedPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.fileLockedPanel.Controls.Add(this.encryptionKeyPlaceholder);
this.fileLockedPanel.Controls.Add(this.fileLockedCloseButton);
this.fileLockedPanel.Controls.Add(this.fileLockedOkButton);
this.fileLockedPanel.Controls.Add(this.fileLockedKeyTextBox);
@ -1073,19 +1071,6 @@ public void InitializeComponent()
this.fileLockedPanel.Visible = false;
this.fileLockedPanel.VisibleChanged += new System.EventHandler(this.FileLockedPanel_VisibleChanged);
//
// encryptionKeyPlaceholder
//
this.encryptionKeyPlaceholder.AutoSize = true;
this.encryptionKeyPlaceholder.BackColor = System.Drawing.SystemColors.Window;
this.encryptionKeyPlaceholder.Cursor = System.Windows.Forms.Cursors.IBeam;
this.encryptionKeyPlaceholder.Enabled = false;
this.encryptionKeyPlaceholder.ForeColor = System.Drawing.Color.DarkGray;
this.encryptionKeyPlaceholder.Location = new System.Drawing.Point(11, 37);
this.encryptionKeyPlaceholder.Name = "encryptionKeyPlaceholder";
this.encryptionKeyPlaceholder.Size = new System.Drawing.Size(82, 13);
this.encryptionKeyPlaceholder.TabIndex = 12;
this.encryptionKeyPlaceholder.Text = "Encryption key";
//
// fileLockedCloseButton
//
this.fileLockedCloseButton.Cursor = System.Windows.Forms.Cursors.Hand;
@ -1125,6 +1110,10 @@ public void InitializeComponent()
this.fileLockedKeyTextBox.Size = new System.Drawing.Size(231, 22);
this.fileLockedKeyTextBox.TabIndex = 8;
this.fileLockedKeyTextBox.UseSystemPasswordChar = true;
this.fileLockedKeyTextBox.Placeholder = "Encryption key";
this.fileLockedKeyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray;
this.fileLockedKeyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F);
this.fileLockedKeyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray;
this.fileLockedKeyTextBox.TextChanged += new System.EventHandler(this.FileLockedKeyTextBox_TextChanged);
this.fileLockedKeyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FileLockedKeyTextBox_KeyDown);
//
@ -1330,8 +1319,7 @@ public void InitializeComponent()
private System.Windows.Forms.ContextMenuStrip trayMenu;
private System.Windows.Forms.ToolStripMenuItem showTrayMenu;
private System.Windows.Forms.ToolStripMenuItem exitTrayMenu;
private System.Windows.Forms.Label encryptionKeyPlaceholder;
protected internal System.Windows.Forms.TextBox fileLockedKeyTextBox;
protected internal System.Windows.Forms.PlaceholderTextBox fileLockedKeyTextBox;
protected internal System.Windows.Forms.Panel richTextBoxPanel;
protected internal System.Windows.Forms.Label searchFindNextButton;
protected internal System.Windows.Forms.Label searchCloseButton;

View file

@ -1625,11 +1625,9 @@ private void FileLockedKeyTextBox_TextChanged(object sender, EventArgs e)
if (fileLockedKeyTextBox.Text.Length > 0)
{
fileLockedOkButton.Enabled = true;
encryptionKeyPlaceholder.Visible = false;
}
else
{
encryptionKeyPlaceholder.Visible = true;
fileLockedOkButton.Enabled = false;
}
}

View file

@ -0,0 +1,257 @@
using System.Drawing;
using System.ComponentModel;
namespace System.Windows.Forms
{
public class PlaceholderTextBox : TextBox
{
#region Fields
#region Protected Fields
protected string _placeholderText = "Default placeholder"; //The placeholder text
protected Color _placeholderColor; //Color of the placeholder when the control does not have focus
protected Color _placeholderActiveColor; //Color of the placeholder when the control has focus
#endregion
#region Private Fields
private Panel placeholderContainer; //Container to hold the placeholder
private Font placeholderFont; //Font of the placeholder
private SolidBrush placeholderBrush; //Brush for the placeholder
#endregion
#endregion
#region Constructors
public PlaceholderTextBox()
{
Initialize();
}
#endregion
#region Private Methods
/// <summary>
/// Initializes placeholder properties and adds CtextBox events
/// </summary>
private void Initialize()
{
//Sets some default values to the placeholder properties
_placeholderColor = Color.LightGray;
_placeholderActiveColor = Color.Gray;
placeholderFont = Font;
placeholderBrush = new SolidBrush(_placeholderActiveColor);
placeholderContainer = null;
//Draw the placeholder, so we can see it in design time
DrawPlaceholder();
//Eventhandlers which contains function calls.
//Either to draw or to remove the placeholder
Enter += new EventHandler(ThisHasFocus);
Leave += new EventHandler(ThisWasLeaved);
TextChanged += new EventHandler(ThisTextChanged);
}
/// <summary>
/// Removes the placeholder if it should
/// </summary>
private void RemovePlaceholder()
{
if (placeholderContainer != null)
{
Controls.Remove(placeholderContainer);
placeholderContainer = null;
}
}
/// <summary>
/// Draws the placeholder if the text length is 0
/// </summary>
private void DrawPlaceholder()
{
if (placeholderContainer == null && TextLength <= 0)
{
placeholderContainer = new Panel(); // Creates the new panel instance
placeholderContainer.Paint += new PaintEventHandler(placeholderContainer_Paint);
placeholderContainer.Invalidate();
placeholderContainer.Click += new EventHandler(placeholderContainer_Click);
Controls.Add(placeholderContainer); // adds the control
}
}
#endregion
#region Eventhandlers
#region Placeholder Events
private void placeholderContainer_Click(object sender, EventArgs e)
{
Focus(); //Makes sure you can click wherever you want on the control to gain focus
}
private void placeholderContainer_Paint(object sender, PaintEventArgs e)
{
//Setting the placeholder container up
placeholderContainer.Location = new Point(2, 0); // sets the location
placeholderContainer.Height = Height; // Height should be the same as its parent
placeholderContainer.Width = Width; // same goes for width and the parent
placeholderContainer.Anchor = AnchorStyles.Left | AnchorStyles.Right; // makes sure that it resizes with the parent control
if (ContainsFocus)
{
//if focused use normal color
placeholderBrush = new SolidBrush(_placeholderActiveColor);
}
else
{
//if not focused use not active color
placeholderBrush = new SolidBrush(_placeholderColor);
}
//Drawing the string into the panel
Graphics g = e.Graphics;
g.DrawString(_placeholderText, placeholderFont, placeholderBrush, new PointF(-2f, 1f));//Take a look at that point
//The reason I'm using the panel at all, is because of this feature, that it has no limits
//I started out with a label but that looked very very bad because of its paddings
}
#endregion
#region CTextBox Events
private void ThisHasFocus(object sender, EventArgs e)
{
//if focused use focus color
placeholderBrush = new SolidBrush(_placeholderActiveColor);
//The placeholder should not be drawn if the user has already written some text
if (TextLength <= 0)
{
RemovePlaceholder();
DrawPlaceholder();
}
}
private void ThisWasLeaved(object sender, EventArgs e)
{
//if the user has written something and left the control
if (TextLength > 0)
{
//Remove the placeholder
RemovePlaceholder();
}
else
{
//But if the user didn't write anything, Then redraw the control.
Invalidate();
}
}
private void ThisTextChanged(object sender, EventArgs e)
{
//If the text of the textbox is not empty
if (TextLength > 0)
{
//Remove the placeholder
RemovePlaceholder();
}
else
{
//But if the text is empty, draw the placeholder again.
DrawPlaceholder();
}
}
#region Overrided Events
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Draw the placeholder even in design time
DrawPlaceholder();
}
protected override void OnInvalidated(InvalidateEventArgs e)
{
base.OnInvalidated(e);
//Check if there is a placeholder
if (placeholderContainer != null)
//if there is a placeholder it should also be invalidated();
placeholderContainer.Invalidate();
}
#endregion
#endregion
#endregion
#region Properties
[Category("Placeholder attribtues")]
[Description("Sets the text of the placeholder")]
public string Placeholder
{
get { return _placeholderText; }
set
{
_placeholderText = value;
Invalidate();
}
}
[Category("Placeholder attribtues")]
[Description("When the control gaines focus, this color will be used as the placeholder's forecolor")]
public Color PlaceholderActiveForeColor
{
get { return _placeholderActiveColor; }
set
{
_placeholderActiveColor = value;
Invalidate();
}
}
[Category("Placeholder attribtues")]
[Description("When the control looses focus, this color will be used as the placeholder's forecolor")]
public Color PlaceholderForeColor
{
get { return _placeholderColor; }
set
{
_placeholderColor = value;
Invalidate();
}
}
[Category("Placeholder attribtues")]
[Description("The font used on the watermark. Default is the same as the control")]
public Font PlaceholderFont
{
get
{
return placeholderFont;
}
set
{
placeholderFont = value;
Invalidate();
}
}
#endregion
}
}