Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

Silverlight Password Box

Rate me:
Please Sign up or sign in to vote.
3.50/5 (9 votes)
30 Jun 2008CPOL 65K   745   29   10
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).

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader GOYELLO
Poland Poland
I started work with .Net in year 2005. During my studies I was a team leader of project which scored 4th place on Polish Imagine Cup Finals where we delivered WPF application with https WCF services. Meanwhile I have worked on Windows Mobile Applications with Windows CE 5.0. I have learned how to create Windows CE Images using Platform Builder and I gained high skills in Compact Framework 2.0. After one year of developing using this framework I have switched to ASP.Net. Currently I am using Monorail Framework together with Active Record and Extjs library (for presentation layer). I am a Team Leader in GOYELLO where we delivered quite successful projects. My aim is to develop good quality applications but not forgetting about reality. Always there are some drawbacks on each solution. The case is to find the proper balance Smile | :)

Comments and Discussions

 
Question[My vote of 2] What about the cursor???? Pin
Sike Mullivan27-May-10 7:02
Sike Mullivan27-May-10 7:02 
GeneralPasswordBox Control Pin
Peace ON10-May-10 0:36
Peace ON10-May-10 0:36 
GeneralSome changes for given code [modified] Pin
introsas19-Sep-08 7:24
introsas19-Sep-08 7:24 
QuestionConnection with the server? Pin
sydaze10-Sep-08 3:23
sydaze10-Sep-08 3:23 
GeneralWorks just fine, just add "__textBox.Select(__textBox.Text.Length, 1);" to the end ... Pin
ericsson00722-Aug-08 14:16
ericsson00722-Aug-08 14:16 
.. of the mthod to ensure that cursor stays at the end of the text box while typing data.
Thanks!
GeneralIl y a beaucoup plus simple Pin
mandark.dev14-Aug-08 10:04
mandark.dev14-Aug-08 10:04 
QuestionSilverlight can use build-in html control? Pin
Jcmorin1-Jul-08 3:55
Jcmorin1-Jul-08 3:55 
AnswerRe: Silverlight can use build-in html control? Pin
Maciej Gren1-Jul-08 10:35
Maciej Gren1-Jul-08 10:35 
Generalvalidating User Name & Password Pin
248912830-Jun-08 20:03
248912830-Jun-08 20:03 
GeneralRe: validating User Name & Password Pin
Maciej Gren1-Jul-08 7:34
Maciej Gren1-Jul-08 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.