Introduction
While digging into Silverlight 2 Beta 2, I have noticed that there is no Password Box available. It will be in the final version, but till then we have to figure out some other way to do it. This solution is quick and maybe even a little bit dirty but it works and that's what makes it valuable enough to be posted. :)
Using the Code
Just attach the tbPassword_TextChanged
method (from the attached example) to your password text box (simple Silverlight Text Box).
string _currentText = "";
void tbPassword_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox __textBox = sender as TextBox;
if (__textBox != null)
{
string __currentText = __textBox.Text;
if (__currentText.Length < _currentText.Length)
_currentText = _currentText.Substring(0, __currentText.Length);
if (__currentText != "")
{
for (int i = 0; i < __currentText.Length; i++)
{
if (__currentText[i] != '\u25CF')
{
string __temp = __currentText.Remove(i, 1);
__textBox.Text = __temp.Insert(i, "\u25CF");
_currentText = _currentText.Insert
(_currentText.Length, __currentText[i].ToString());
}
}
}
}
}
In the _currentText
, you will find text from the password box.
Points of Interest
I am working on quite a big RIA Silverlight application. Lacking this basic password box made my stomach ache. Hopefully, in the final release of Silverlight, we won't have such issues anymore.
History
While creating this small sample, I was inspired by Michael Sync's post. Unfortunately, Silverlight 2 beta 2 sample source code did not work properly and that is why I decided to write a simpler solution (IMHO).