Click here to Skip to main content
Email Password   helpLost your password?
PasswordBox

Introduction

While digging into Silverlight 2 Beta 2 I have noticed that there is no Password Box available. It will be in a finall 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 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 big RIA Silverlight application. Lacking this basic password box made my stomach aches. Hopefully in final release of Silverlight we wont have such issues anymore.

History

While creating this small sample I was inspired by Michael Sync post. Unfortunatly, Silverlight 2 beta 2 sample source code did not work properly that is why I decided to write simplier solution (IMHO).

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSome changes for given code [modified]
introsas
8:24 19 Sep '08  
Here is my updated code


public class PasswordBox : TextBox
{
#region Private Member Variables

private string _text = string.Empty;
private char _passwordChar = '\u25CF';

#endregion

#region Constructor

/// <summary>
/// Default Class Constructor
/// </summary>
public PasswordBox()
{
this.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
}


#endregion


#region Private Methods


private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox myTxtBox = sender as TextBox;

if (myTxtBox != null)
{
string currentText = myTxtBox.Text;
int selStart = myTxtBox.SelectionStart;

if (currentText.Length < _text.Length)
{
// Remove deleted chars
_text = _text.Remove(selStart, _text.Length - currentText.Length);
}
if (currentText != "")
{
for (int i = 0; i < currentText.Length; i++)
{
if (currentText[i] != _passwordChar)
{
// Unattach event handler
myTxtBox.TextChanged -= new TextChangedEventHandler(TextBox_TextChanged);
myTxtBox.Text = myTxtBox.Text.Remove(i, 1).Insert(i, _passwordChar.ToString());
myTxtBox.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
// Replace or insert char
_text = myTxtBox.Text.Length == _text.Length ?
_text.Remove(i, 1).Insert(i, currentText[i].ToString()) :
_text.Insert(i, currentText[i].ToString());
}
}
}
myTxtBox.SelectionStart = selStart;
}
}

#endregion
#region Properties

public new string Text
{
get { return _text; }
}

#endregion
}




modified on Sunday, September 21, 2008 6:36 AM

QuestionConnection with the server?
sydaze
4:23 10 Sep '08  
Hi, there's something I still don't understand with silverlight:
When I created this login box, what are the solutions to send the data (login/password) to the servers and receive an answer to know if it's correct or not.
I read about web services but is it the only solution for silverlight to communicate with the server?
GeneralWorks just fine, just add "__textBox.Select(__textBox.Text.Length, 1);" to the end ...
ericsson007
15:16 22 Aug '08  
.. 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
mandark.dev
11:04 14 Aug '08  
Il y a beaucoup plus simple que recoder le controle :
http://dev-tricks.net/silverlight-password-field-no-longer-missing[^
Donne une solution avec 0 ligne de code, enjoyez Smile
GeneralSilverlight can use build-in html control?
Jcmorin
4:55 1 Jul '08  
I read somewhere that silverlight can use the basic html control... is that true?
if so you could use the html one?
GeneralRe: Silverlight can use build-in html control?
Maciej Gren
11:35 1 Jul '08  
I have prepared an small article about it:
http://www.codeproject.com/KB/silverlight/silverligth2b2javascripts.aspx
Generalvalidating User Name & Password
2489128
21:03 30 Jun '08  
How to validate User Name & Password from the database using this control.
GeneralRe: validating User Name & Password
Maciej Gren
8:34 1 Jul '08  
This will be my next step. This example presents how to setup quick solution for password box control in Silverlight 2 beta 2. In final release they said that we will have good to go PasswordBox so I dont put too much effort to wrap it into control. When I will establish communication with SL and WCF then I will post it for you.


Last Updated 30 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010