C# Programming > Windows Forms

C# System Password Textbox

Password Textbox

A C# Password Textbox is very simple to add to .NET applications. However there are two common ways to implement a textbox with password character and one way can look better than the other.

It's a matter of working with TextBox properties.

PasswordChar

The first way to create a C# password textbox is to use the PasswordChar property. PasswordChar in a Textbox is set to a single character. Then whenever the user types text into the textbox, the actual characters are masked by the PasswordChar:

C# Password Textbox

Accessing the actual data in the Textbox is still done with the Text property. Also note that the Textbox will automatically act as a password textbox, meaning command such as Ctrl Copy are disabled.

The advantage is the flexibility to specify any character in PasswordChar.

UseSystemPasswordChar

The second, and better, way to create a C# password textbox is to use the UseSystemPasswordChar property. This property is a bool value that is switched to true to make any textbox act as a password textbox. The difference is that characters will be masked with a PasswordChar set by the system, which in the case of .NET will be the version of windows. So in Vista for example, you get something like this:

C# System Password Textbox

The actual string in the textbox is also accessed through Text property and Ctrl Copy is also automatically disabled. The advantage is that the password textbox will look more like part of the system.

 

Back to C# Article List