Search code examples
delphidelphi-xe8

PasswordChar in Delphi XE8's TMemo


I spent a few hours searching Google to see if anyone had shared their articles, but came up empty-handed.

If it's possible, I want to know how to enable/disable the PasswordChar in Delphi XE8's TMemo to hide user input like in TEdit. ? Maybe via a checkbox!

So when the checkbox is checked then all text turned to asterisks, and if the checkbox is unchecked, all text back to normal..


Solution

  • The VCL memo control is a loose wrapper around the Win32 multiline edit. The password character functionality of the edit control is only available for single line edits.

    The behaviour is controlled by the ES_PASSWORD style for which the documentation says:

    Displays an asterisk (*) for each character typed into the edit control. This style is valid only for single-line edit controls.

    The FMX memo control offers no password character functionality for the multiline memo control.

    Presumably these frameworks don't offer what you want because passwords are entered in single line edit controls. Developers tend not to provide functionality that has no clear case for being used.

    Your options:

    • Use a single line TEdit.
    • Write your own multiline memo that supports your desired functionality.
    • Find a third party multiline memo that supports your desired functionality.

    Now, since your question is so general I have assumed that you want full support for single line password character. That is, the user enters text and it appears masked.

    But maybe you actually don't need editability. In that case it is simple enough. Do the following:

    1. Load or add the true text into a separate TStringList.
    2. When you want to display the true text assign the string list to the memo.
    3. When you want to hide the content, process the true text into whatever you want to display, and display that.
    4. Make the memo control read only.