The solution: (also see
http://blog.tcx.be/2008/04/making-your-password-textbox-more.html[
^])
#pragma once
#include <Windows.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Security::Permissions;
namespace InputForms
{
[ToolboxItem(true), ToolboxBitmap(TextBox::typeid)]
public ref class pwTextBox : public System::Windows::Forms::TextBox
{
public:
pwTextBox(void) : TextBox()
{
this->_bAccessText = false;
this->UseSystemPasswordChar = true;
};
property String^ Text
{
virtual String^ get(void) override
{
this->_bAccessText = true;
try { return TextBox::Text; }
finally { this->_bAccessText = false; }
}
virtual void set(String^ value) override
{
this->_bAccessText = true;
try { TextBox::Text = value; }
finally { this->_bAccessText = false; }
}
};
property int TextLength
{
virtual int get(void) override
{
this->_bAccessText = true;
try { return TextBox::TextLength; }
finally { this->_bAccessText = false; }
}
};
protected:
~pwTextBox()
{
}
[SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
virtual void WndProc(Message% m) override
{
switch (m.Msg)
{
case WM_GETTEXT:
case WM_GETTEXTLENGTH:
if (!this->_bAccessText)
{
m.Result = IntPtr::Zero;
return;
}
else break;
case EM_SETPASSWORDCHAR: return;
}
TextBox::WndProc(m);
};
private:
bool _bAccessText;
};
}