Share via


Password TextBox tidbits

Been away for a while....thought I'd post some stuff I learned while I was away from blogging.

I had a need to display passwords in a DataGrid or list format. The credentials were in a name-value pair XML config file as part of a larger schema, where the password nodes were encrypted:

<Credentials>
<Credential key="UserId">myuserid</Credential>
<Credential key="Password"></Credential>
</Credentials>

As you would find out, it is pretty easy to mask the password columns using the CellFormatting event to replace the actual password value with the mask. However, you might notice that this is different from the characters used by the TextBox when you set the UseSystemPasswordChar=true. Usually we use something like an * (asterik), but the SystemPasswordChar is a something closer to a bullet with some kerning between characters.

Turns out (after some digging and asking questions) the password character is hardcoded into the system (seems odd, but c'est la vie). You can access it using somthing like:

 TextBox textBox = new TextBox();
textBox.UseSystemPasswordChar = true; 
char sysPasswordChar = textBox.PasswordChar;
textBox.Dispose(); 

Now you can use the system password char in the CellFormatting event, or any place else, to make your controls mask passwords just like a TextBox would.