Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

CEdit - Show or Hide Password

4.24/5 (8 votes)
13 Feb 2016CPOL 17.8K  
To dynamically set the password viewing style for the edit control

Introduction

This code shows you how to dynamically change the edit control style specifically for the password feature..

Background

Being familiar with the CEdit class would be useful.

Using the Code

I had a need to change the password style on an edit control dynamically. The following code shows how this can be done:

C++
// ... where m_CtrlEditData is a CEdit control variable...

// On button click or other event/command - hide the input
m_CtrlEditData.SetPasswordChar('*');
m_CtrlEditData.Invalidate();

// On another button click or event/command - show the input
m_CtrlEditData.SetPasswordChar(0);
m_CtrlEditData.Invalidate();

Points of Interest

The call to Invalidate() was necessary, otherwise you may simply not see any change.

SetPasswordChar with a 0, not a '0' to show user input. In other words, SetPasswordChar('0') does not work, but SetPasswordChar(0) does work.

History

  • Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)