none
How to set mode Password for TextBox control?

    Question

  • I want to set TextBox to password mode (with * for each character). I can not find out how to implement this feature. Please help me...

    Thanks.

    Friday, June 23, 2006 7:32 AM

Answers

  • The TextBox doesn't have a password mode. Instead, there's a special control for this: PasswordBox.

    I think the main reason for this is that TextBox supports a bunch of features that you almost certainly don't want in a password box, like multiline text, typography, text decorations.

    Friday, June 23, 2006 8:44 AM
  • Use PasswordBox control and set PasswordChar as '*'
    Friday, June 23, 2006 12:44 PM
    Moderator
  • the passwordbox doesnt support it....

    You are really looking for a masked textbox which isnt supported in the current version.  .. an alternative is to use the Winforms MaskedTextBox.

    Wednesday, April 18, 2007 8:13 PM
    Moderator

All replies

  • The TextBox doesn't have a password mode. Instead, there's a special control for this: PasswordBox.

    I think the main reason for this is that TextBox supports a bunch of features that you almost certainly don't want in a password box, like multiline text, typography, text decorations.

    Friday, June 23, 2006 8:44 AM
  • Use PasswordBox control and set PasswordChar as '*'
    Friday, June 23, 2006 12:44 PM
    Moderator
  • Thanks guy,

    It works fine... excellent!

    Monday, June 26, 2006 12:42 AM
  • Is there any way to monitor the caret index of a passwordbox like you can in a textbox?
    Thursday, April 05, 2007 6:04 PM
  • the passwordbox doesnt support it....

    You are really looking for a masked textbox which isnt supported in the current version.  .. an alternative is to use the Winforms MaskedTextBox.

    Wednesday, April 18, 2007 8:13 PM
    Moderator
  • I also tried this Control, PasswordBox, but I encountered a problem. I wand to use this control in a custom made LoginForm. So this PasswordBox should not only contain the same number of characters as I type, I should be able to read those characters in order to validate the Login attempt. It doesn't have the Text property, I can only use ToString, but it's not helpful.

    Here is my Code:

    1If txtUserName.Text = "" Then 
    2            MsgBox("Please supply a username!", MsgBoxStyle.Information, "Login"
    3            txtUserName.Focus() 
    4            Exit Sub 
    5        End If 
    6        If pwdPassword.ToString = "" Then 
    7            MsgBox("Please supply a password!", MsgBoxStyle.Information, "Login"
    8            pwdPassword.Focus() 
    9            Exit Sub 
    10        End If 
    11 
    12 
    13        Dim mCS As String = "server=******;database=*******;UID=******;pwd=******" 
    14        Dim mConn As New SqlConnection(mCS) 
    15        Dim mCmd As SqlCommand 
    16        Dim mRdr As SqlDataReader = Nothing 
    17        Dim sql As String = "" 
    18 
    19        mConn.Open() 
    20 
    21        sql = "Select name, nickname, password, userid from users (nolock)" _ 
    22                & "where nickname = '" & txtUserName.Text & "' and valid = -1 " 
    23 
    24        mCmd = New SqlCommand(sql, mConn) 
    25        mRdr = mCmd.ExecuteReader 
    26        If mRdr.Read Then 
    27            If Decript(mRdr(2)) <> pwdPassword.ToString Then 
    28                MsgBox("Wrong Password", MsgBoxStyle.Information, "Login"
    29                pwdPassword.Clear() 
    30                pwdPassword.Focus() 
    31                mRdr.Close() 
    32                mCmd.Dispose() 
    33                mConn.Close() 
    34                mConn.Dispose() 
    35                bLogin = False 
    36                myFullName = "" 
    37                Exit Sub 
    38            Else 
    39                Nume = mRdr(0) 
    40                NickName = mRdr(1) 
    41                PassCript = mRdr(2) 
    42                UserId= mRdr(3) 
    43                 
    44                Dim f As New Window 
    45                f = New winMain() 
    46                f.ShowDialog() 
    47 
    48                mRdr.Close() 
    49                mCmd.Dispose() 
    50                mConn.Close() 
    51                mConn.Dispose() 
    52            End If 
    53        Else 
    54            MsgBox("You did not earn your way to use this application", MsgBoxStyle.Information, "Login"
    55            txtUserName.Text = "" 
    56            txtUserName.Focus() 
    57            mRdr.Close() 
    58            mCmd.Dispose() 
    59            mConn.Close() 
    60            mConn.Dispose() 
    61            bLogin = False 
    62            Exit Sub 
    63        End If 
    64 
    65 
    66        myPass = pwdPassword.ToString 
    67        bLogin = True 
    68 
    69        Me.Close() 


    Thanks in advance!
    7th Sound
    Wednesday, June 11, 2008 8:53 PM
  • You can also look for a third party WPF control like the one included on the Xceed Datagrid, the free edition. Has numeric, date and masked text boxes.
    Sunday, September 07, 2008 7:44 PM
  • Try the Password property.

    myPass = pwdPassword.Password;
    Monday, November 17, 2008 12:27 PM
  • public
     class
     TextBoxEx
     : TextBox
    
        {       
            private  Brush  focusColororiginalBgColor ;
            private  bool  isPassword  =  false ;
            private  List < char >  pwrdChars  =  new  List < char > ();
            private  FontFamily  fam  =  new  FontFamily ("Wingdings" );
            private  FontFamily  origFam ;
                        
            //Wingdings 
            public  TextBoxEx ()
            {
                this . FlowDirection  =  System . Windows . FlowDirection . LeftToRight ;
                this . TextChanged  +=  new  TextChangedEventHandler (TextBoxEx_TextChanged );
            }
            public  override  void  EndInit ()
            {
                base . EndInit ();
                this . origFam  =  this . FontFamily ;
            }
            private  void  TextBoxEx_TextChanged (object  senderTextChangedEventArgs  e )
            {
                if  (isPassword )
                {
                    FontFamily  =  fam ;
                    char  c  =  Text [Text . Length  -  1 ];
                    pwrdChars . Add (c );
                    this . TextChanged  -=  TextBoxEx_TextChanged ;
                    string  str  =  Text ;
                    Text  =  Text . Replace (c'l' );
                    SelectionStart  =  Text . Length ;
                    this . TextChanged  +=  TextBoxEx_TextChanged ;
                }
                else
                    this . FontFamily  =  origFam ;
            }
          
            protected  override  void  OnGotFocus (RoutedEventArgs  e )
            {
                base . OnGotFocus (e );
                this . originalBgColor  =  this . Background ;
                this . Background  =  this . focusColor ;
            }      
            protected  override  void  OnLostFocus (RoutedEventArgs  e )
            {
                base . OnLostFocus (e );
                this . Background  =  originalBgColor ;          
            }       
         
            [Category ("Focus" ), DesignerSerializationVisibility (DesignerSerializationVisibility . Content )]
            public  Brush  FocusColor
            {
                get  
                {
                    if  (this . focusColor  ==  null )
                        this . focusColor  =  this . Background ;
                    return  this . focusColor
                }
                set
                {
                    this . focusColor  =  value ;
                }
            }    
            //[Category("Other")]
            //public override FontFamily FontFamily
            //{
            //    get { return base.FontFamily; }
            //    set { base.FontFamily = this.origFam = value; }
            //}
            [Category ("Common" )]
            public  bool  IsPassword
            {
                get  { return  this . isPassword ; }
                set  
                {        
                    if  (this . isPassword  ==  value )
                        return ;

                    this . isPassword  =  value ;
                    this . TextChanged  -=  this . TextBoxEx_TextChanged ;    
                   
                    if  (this . isPassword )
                    {                     
                        if  (! string . IsNullOrEmpty (this . Text ))
                        {   
                            this . pwrdChars . Clear ();                                                                        
                            for  (int  i  =  0i  <  this . Text . Lengthi ++ )
                            {
                                this . pwrdChars . Add (this . Text [i ]);
                                this . Text  =  this . Text . Replace (Text [i ], 'l' );
                            }                       
                        }
                    }
                    else
                    {
                        this . FontFamily  =  origFam ;
                        if  (this . pwrdChars . Count  >  0 )
                        {                       
                            this . Text  =  new  string (this . pwrdChars . ToArray ());
                            this . pwrdChars . Clear ();
                        }
                    }
                    this . TextChanged  +=  this . TextBoxEx_TextChanged ;
                }
            }
            [Category ("Common" )]
            public  string  Password
            {
                get
                {
                    return  (isPassword?  new  string (pwrdChars . ToArray ()) : null ;
                }
            }
        }

    This is my custom TextBox control which I use for focused/lost focused Background and for Passwords. Originally I would have liked to inherit from
    the PasswordBox class but it is sealed. So you can use this if you would like.
    Sunday, December 05, 2010 8:29 PM
  • First set UseSystemPasswordCh property as TRUE. Then set PasswordChar property as "."  
    Sunday, March 13, 2016 7:27 PM