Wednesday, January 17, 2007

Password TextBox in WPF

In .NET Framework 3.0 (WPF) Microsoft have removed the property of a TextBox, which specifies whether the textbox is Password or Normal type. But in replacement they've introduced a new control named PasswordBox which is very simple and intuitive to use. Here is a simple example:
XAML:
<PasswordBox x:Name="txtPassword"
         ToolTip="Password"
         PasswordChar="*"
    />

By default the PasswordChar is set to the big black dot from Windows XP style logon and.
In order to detect changes in the password you must handle the PasswordChanged event:

VB Codebehind:
Private Sub txtPassword_PasswordChanged(ByVal sender As System.Object _
                  , ByVal e As System.Windows.RoutedEventArgs _
                 ) Handles txtPassword.PasswordChanged
      'Validate input
End Sub

An in order to retrieve the typed password you just use the Password property of the PasswordBox control:

VB Codebehind:
Public Function IsAuthenticated() as Boolean
   Return txtPassword.Password <> ""
End Function

15 comments:

  1. Anonymous11:50 PM

    Thanks, that was helpful.

    ReplyDelete
  2. Anonymous2:31 PM

    That was useful for me also! Thanks!

    ReplyDelete
  3. Anonymous10:52 PM

    Thanks...

    ReplyDelete
  4. Hi there, thanks for that. :)

    ReplyDelete
  5. You should use
    Return (Not String.IsNullOrEmpty(txtPassword.password))

    ReplyDelete
  6. Anonymous12:56 PM

    YerReally helpful

    ReplyDelete
  7. Anonymous3:59 PM

    Thanks a ton

    ReplyDelete
  8. Anonymous3:07 PM

    Really stupid

    ReplyDelete
  9. Thank you very much, you helped me!

    ReplyDelete
  10. Anonymous12:35 PM

    Thank you

    ReplyDelete
  11. Yawar Sohail7:37 AM

    Thanks for this

    ReplyDelete
  12. Anonymous12:43 AM

    you have to add the control manually, by going to
    Tools>Choose Toolbox Items...>.NET Framework Components>

    Check "Password Box", then you will be able to see this on the toolbox

    ReplyDelete
  13. Thanks that helped me

    ReplyDelete
  14. Thanks, this helped me a lot!

    ReplyDelete