WinForms
TextBox
password input
C# programming
UI development

How to set a TextBox for password input in WinForms?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In WinForms, a TextBox becomes a password field by masking the characters the user types. The normal way to do that is to set UseSystemPasswordChar to true, or set a custom PasswordChar if you want a specific masking symbol.

The Simplest Option

If you are using the designer, select the TextBox and set:

  • 'UseSystemPasswordChar = true'

That tells WinForms to display the system's standard password masking character instead of the real text. The underlying Text property still contains the actual password string, so masking is only a UI behavior.

Programmatic Example

Here is the same setup in code:

csharp
1using System;
2using System.Windows.Forms;
3
4public class LoginForm : Form
5{
6    private readonly TextBox passwordBox = new TextBox();
7
8    public LoginForm()
9    {
10        passwordBox.Left = 20;
11        passwordBox.Top = 20;
12        passwordBox.Width = 200;
13        passwordBox.UseSystemPasswordChar = true;
14
15        Controls.Add(passwordBox);
16    }
17}

This is usually the best default because it respects the platform's normal masking behavior.

Using a Custom Mask Character

If you need a specific symbol, set PasswordChar instead:

csharp
passwordBox.PasswordChar = '*';

That forces each typed character to display as *. In many applications, UseSystemPasswordChar is preferable because it matches system conventions, but PasswordChar remains useful when you want explicit visual control.

Add a Show Password Toggle

Users sometimes make mistakes when they cannot see what they typed. A common pattern is a checkbox that temporarily reveals the value:

csharp
1using System;
2using System.Windows.Forms;
3
4public class LoginForm : Form
5{
6    private readonly TextBox passwordBox = new TextBox();
7    private readonly CheckBox showPassword = new CheckBox();
8
9    public LoginForm()
10    {
11        passwordBox.Left = 20;
12        passwordBox.Top = 20;
13        passwordBox.Width = 200;
14        passwordBox.UseSystemPasswordChar = true;
15
16        showPassword.Left = 20;
17        showPassword.Top = 55;
18        showPassword.Text = "Show password";
19        showPassword.CheckedChanged += (_, _) =>
20        {
21            passwordBox.UseSystemPasswordChar = !showPassword.Checked;
22        };
23
24        Controls.Add(passwordBox);
25        Controls.Add(showPassword);
26    }
27}

This improves usability without changing the core password-input behavior.

Masking Is Not Security by Itself

A password box hides characters on screen, but it does not secure the string in memory or make the application safe automatically. Once you read passwordBox.Text, you still need to handle the value carefully:

  • avoid logging it
  • clear it when you are done
  • send it over secure channels
  • use secure authentication APIs instead of storing raw passwords

The UI control helps prevent shoulder surfing. It does not replace proper credential handling.

Designer Settings That Commonly Matter

Alongside password masking, a few other properties are often useful:

  • 'MaxLength to limit input length'
  • 'ShortcutsEnabled if you want to allow or restrict clipboard shortcuts'
  • 'TextAlign for presentation'
  • 'TabIndex so keyboard navigation feels correct'

These are not specific to passwords, but they are part of making the field behave well in a real form.

Common Pitfalls

  • Setting only the font or colors does not make a TextBox behave like a password field; masking requires UseSystemPasswordChar or PasswordChar.
  • Assuming masking makes the password secure is incorrect, because the actual text still exists in memory.
  • Using PasswordChar and then forgetting about accessibility or platform conventions can create inconsistent UI behavior.
  • Logging passwordBox.Text during debugging is an easy way to leak credentials accidentally.
  • Clearing the form visually but leaving the value in application state defeats the point of careful password handling.

Summary

  • In WinForms, set UseSystemPasswordChar = true for a standard password text box.
  • Use PasswordChar only when you need a custom masking symbol.
  • A show-password toggle is easy to add and often improves usability.
  • Password masking is only a display feature, so the real security work still happens in how you handle the entered text.

Course illustration
Course illustration

All Rights Reserved.