[Silverlight 2] Une PasswordBox en Silverlight à utiliser dans vos projets
La contrôle PasswordBox est sans doute l'un des plus importants disponible avec le Framework .NET.
Non présent dans Silverlight 2, je m'en suis créé un pour les besoins d'un projet. Pour le faire, j'ai simplement hériter de la classe TextBox: on voit donc que le modèle de développement est le même qu'en WPF (Windows Presentation Foundation) 
Bien sur, ce contrôle n'est pas parfait, en grande partie à cause du fait que le contrôle TextBox ne donne aucune information sur la position du curseur (Caret): sans doute pour la version finale ? 
Quoiqu'il en soit, ce contrôle fonctionne et vous donne une idée de comment tout cela fonctionne.
/// <summary>
/// A control that represent a Password TextBox. As there is no information about the position of the caret,
/// the control is not perfect but all the logic is here.
/// </summary>
public class PasswordBox : TextBox
{
public StringBuilder SecureText { get; private set; }
public PasswordBox()
: base()
{
this.SecureText = new StringBuilder();
this.KeyDown += new KeyEventHandler(PasswordBox_KeyDown);
}
void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Back)
{
this.SecureText.Append(e.Key.ToString().ToLower());
this.Text = string.Empty;
for (int i = 0; i <= this.SecureText.Length - 1; i++)
{
this.Text += "*";
}
}
else
{
this.SecureText.Remove(this.SecureText.Length - 1, 1);
this.Text.Remove(this.Text.Length - 1, 1);
}
e.Handled = true;
}
}
A+
Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin :