开发者

Password char for RichTextBox

I am using transparent custom control based on RichTextBox. Is there are any way to have password char support like in regular TextBox contro开发者_开发知识库l.


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;
        }
    }
}


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.


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;
}


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜