Reputation: 33
I am using transparent custom control based on RichTextBox. Is there are any way to have password char support like in regular TextBox control.
Upvotes: 1
Views: 5237
Reputation: 41
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;
}
Upvotes: 0
Reputation: 85
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
Upvotes: -1
Reputation: 942050
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;
}
}
}
Upvotes: 6
Reputation: 6627
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.
Upvotes: 0