|
// -------------------------------------------------------
// SqlBuilder by Elm�Soft
// www.netcult.ch/elmue
// www.codeproject.com/KB/database/SqlBuilder.aspx
// -------------------------------------------------------
using System;
using System.Text;
using System.Windows.Forms;
using SqlBuilder.Forms;
namespace SqlBuilder.Controls
{
/// <summary>
/// This class is a textbox which displays a password
/// The password is displayed as plain text while it is typed in by the user.
/// But it is displayed as 12 stars when set via SetText()
/// Accessing the content is ONLY possible via SetText() and GetText()
/// otherwise (using pwdBox.Text) you will receive only stars !!!
/// This text box can NOT by spied out using a Win32 API spy or a .NET spy
/// </summary>
public class PasswordTextBox : TextBox
{
const string ms_Stars = "************";
private string ms_Encrypted = "";
/// <summary>
/// Use SetText() instead of the ."Text" property
/// Only properties can be spied with a .NET spy, not functions
/// </summary>
public void SetText(string s_Value, bool b_Encrypted)
{
s_Value = s_Value.Trim();
if (b_Encrypted)
{
// If a password is set which cannot be decrypted -> set empty
if (Functions.Crypt(s_Value, Defaults.EncryptionKey, false) == "")
s_Value = "";
ms_Encrypted = s_Value;
}
else
{
ms_Encrypted = Functions.Crypt(s_Value, Defaults.EncryptionKey, true);
}
if (ms_Encrypted.Length > 0) this.Text = ms_Stars;
else this.Text = "";
base.OnTextChanged (null);
}
/// <summary>
/// Use GetText() instead of the ".Text" property
/// Only properties can be spied with a .NET spy, not functions
/// </summary>
public string GetText(bool b_Encrypted)
{
if (b_Encrypted)
return ms_Encrypted;
else
return Functions.Crypt(ms_Encrypted, Defaults.EncryptionKey, false);
}
protected override void OnTextChanged(EventArgs e)
{
// Do not fire the event here! (this is called before KeyUp!)
}
/// <summary>
/// Refresh the internal value after the user has typed text
/// </summary>
protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyUp(e);
if (base.Text != ms_Stars)
ms_Encrypted = Functions.Crypt(base.Text.Trim(), Defaults.EncryptionKey, true);
base.OnTextChanged (e);
}
/// <summary>
/// After the control lost the focus -> display stars or empty
/// </summary>
protected override void OnLostFocus(System.EventArgs e)
{
base.OnLostFocus(e);
if (ms_Encrypted.Length > 0) this.Text = ms_Stars;
else this.Text = "";
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.