0

I have a login form with two textboxes, one for username and one for password.

I have used a textbox for the password input, however I would like to mask the characters so that rather than showing letters, it will show a (*) symbol or a password dot.

I can not change the textbox to a password box as this throws errors.

Using C#, WPF form

Rogan
  • 47
  • 1
  • 6
  • can you explain what errors you are running into? – Rick Hodder Jan 18 '17 at 19:15
  • there appear to be solutions (http://stackoverflow.com/a/6629237/1132334) but they all look far more complicated than using a password box. would it be worth to sort out that "pool of errors" instead? has it something to do with the `PasswordBox` not inheriting from `TextBox`? – Cee McSharpface Jan 18 '17 at 19:15
  • @dlatikay for example, I am using a SQL database to retrieve the users login details. However, when I use "passwordBox.Text" to retrieve the input from the password box i get errors. – Rogan Jan 18 '17 at 19:23
  • have you tried passwordBox.Password? https://msdn.microsoft.com/en-us/library/system.windows.controls.passwordbox.password(v=vs.110).aspx – sous2817 Jan 18 '17 at 19:30
  • @Rogan What errors are you getting, exactly? Also, are you using the MVVM pattern or any data-binding, or are you doing it old-school (like WinForms) where your code directly reads and assigns control properties? – Dai Jan 18 '17 at 19:37
  • 2
    The `PasswordBox` does not inherit from `TextBox`. Its content is accessible via the `Password` and `SecurePassword` properties, and that's for a reason, see also [documentation](https://msdn.microsoft.com/en-us/library/system.windows.controls.passwordbox(v=vs.110).aspx) do some research, there are at least three googleable SO posts on the topic/add the compiler+runtime errors you get to your question. – Cee McSharpface Jan 18 '17 at 19:42
  • question solved using @dlatikay comment. Thanks – Rogan Jan 19 '17 at 23:41

2 Answers2

4

I would suggest using the PasswordBox and rewriting your code to work with that instead of coming up with a work around. I am not exactly sure what error you are getting, but you can get the value of the PasswordBox almost the same way as getting the value/text from a TextBox.

TextBox:

TextBox1.Text

PasswordBox:

PasswordBox1.Password
PasswordBox1.SecurePassword
RockGuitarist1
  • 698
  • 1
  • 7
  • 16
-1

Try this,

Inside xaml file:-

  <TextBlock Height="20" HorizontalAlignment="Right" x:Name ="Pwderrormessage" VerticalAlignment="Top" Width="156" Margin="0,347,60,0" OpacityMask="Crimson" Foreground="#FFE5572C" />

     <Label HorizontalContentAlignment="Left" Content="Password:" Foreground="White" TextBlock.FontSize="14" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="230,340,0,0" Height="30" Width="84"/>
                <StackPanel Height="23" Width="200" Margin="126,170,0,0">
                    <PasswordBox x:Name="txtpwd" PasswordChar="*" Height="23" Width="200"/>
                    </StackPanel>

And code behind page.

On button click,

   if (txtpwd.Password.Length == 0)
            {
                Pwderrormessage.Text = "Enter a Password.";
                txtpwd.Focus();
            }

            else
            {
                /*Save you data*/
            }

Thanks, Abhilash.J.A

AbhiJA
  • 341
  • 1
  • 2
  • 13