I am using transparent custom control based on RichTextBox. Is there are any way to have password char support like in regular TextBox control.
Asked
Active
Viewed 4,862 times
1
-
4*Why* would you want to enter a password in a `RichTextBox`? It sounds like you're using the wrong control for the job. It's explicitly *not designed* for entering passwords, and could present a security risk. – Cody Gray - on strike Dec 15 '10 at 15:28
-
That was a part of user interface design requirements for the application to have text editing control with gradient background. Security is not an issue, this application has a very limited security requirements. – user976535 Dec 15 '10 at 15:36
-
2Is this a WinForms or WPF application? Any time that you say that "user interface design requirements" trump any kind of actual functionality, that just screams for the app to be written in WPF... – Cody Gray - on strike Dec 15 '10 at 15:43
-
hmm, yes it is a WinForm application – user976535 Dec 15 '10 at 15:50
4 Answers
6
It is simple to do, RTB actually implements support for password entry. It just isn't exposed in the .NET wrapper. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class RichPassword : RichTextBox {
protected override CreateParams CreateParams {
get {
// Turn on ES_PASSWORD
var cp = base.CreateParams;
cp.Style |= 0x20;
return cp;
}
}
}

Hans Passant
- 922,412
- 146
- 1,693
- 2,536
-
1This worked a treat for me... until I updated my app from .NET 4.5.2 to .NET 4.8. Now if I use this RichPassword class object in a form, the form won't ever close when I ask it to. Anyone seen anything similar? – Nick Shaw Sep 24 '19 at 11:17
-
Google "Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl" to find out how to keep using RTB version 2.0 – Hans Passant Sep 24 '19 at 11:21
0
Yes there is a way. You would have to intercept key presses before they are drawn (look for PreviewKeyDown or similar events) and change them to asterisks(*). But you also need to implement logic to manage the real chars string.
Note I am in agreement with the Cody Gray comment.

Liviu Mandras
- 6,540
- 2
- 41
- 65
-
1It's also worth noting that the standard Win32 `TextBox` control does a *lot* more when it's set to store passwords than simply displaying characters as asterisks/bullets. For example, it also disables copying a password out of the textbox. You're about to start rolling a rock up a hill by trying to do all of this yourself, even if your security requirements are "limited". – Cody Gray - on strike Dec 15 '10 at 15:41
-
@Hans: I have unspeakable things in my bag of tricks. Most things are crackable at *some* level if you're willing to try hard enough. I'm just saying, it's far more likely the average user will think to copy and paste a password out of a textbox than they are to have a password revealer. The asker mentioned that security concerns are "limited", so I think that leaves out those who resort to this type of software, but not those who know the Ctrl+C keyboard shortcut. – Cody Gray - on strike Dec 15 '10 at 17:11
0
string Value= "";
int richTextlent = 1;
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
if(richTextBox2.Text.Length == richTextlent)
{
Value += richTextBox2.Text[0].ToString();
richTextBox2.Text = richTextBox2.Text.Remove(0, 1);
richTextBox2.Text += "*";
}
else
{
Value = "";
richTextBox2.Text = "";
}
richTextlent = richTextBox2.Text.Length + 1;
}

shreyasm-dev
- 2,711
- 5
- 16
- 34

JavierQuispe
- 21
- 4
-1
This has worked for me and solved the purpose.
Public Class RichPassword
Inherits RichTextBox
Private Const ES_PASSWORD = 32
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim CP = MyBase.CreateParams
CP.Style = CP.Style Or 32
Return CP
End Get
End Property
End Class

Meghdut Saha
- 85
- 1
- 14