Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight Password Box

0.00/5 (No votes)
30 Jun 2008 1  
A small article about how to create a simple password text box in Silverlight 2 Beta 2
PasswordBox

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).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here