0

I'm trying to make a password eye with WPF, but I can't figure why it does't work.

enter image description here

I want that the passord would be hidden and when user click on the eye the password would be visible.

The idea is to make a hidden textbox and visible passwordbox, and when the user clicks on the eye button the textbox would be visible and the passwordbox would be hidden, here is my code:

XAML:

<PasswordBox x:Name="passwordBox"  HorizontalAlignment="Left" Height="27" Margin="102,37,0,0"  VerticalAlignment="Top" Width="140"/>
                        <TextBox x:Name="password_textbox"  HorizontalAlignment="Left" Height="27" Margin="102,37,0,0"  VerticalAlignment="Top" Width="140" Visibility="Collapsed"/>
                        <Button x:Name="eye_button" Background="White"  HorizontalAlignment="Left" Height="27" Margin="242,37,0,0" VerticalAlignment="Top" Width="30"  MouseDown="MouseDown" MouseUp="MouseUp"/>

Code behind:

      public MyPage()
        {
            InitializeComponent();
            eye_button.PreviewMouseUp += MouseUp;
            eye_button.PreviewMouseUp += MouseDown;           
        }
        .
        .
        .
        void MouseUp(Object sender , RoutedEventArgs args)
        {          
        password_textbox.Text = "aaaaaaaa";
        passwordBox.Visibility = Visibility.Collapsed;
        password_textBox.Visibility = Visibility.Visible;
        password_textBox.Focus();
        }

        void MouseDown(Object sender , RoutedEventArgs args)
        {        
        passwordBox.Visibility = Visibility.Visible;
        password_textBox.Visibility = Visibility.Collapsed;          
        passwordBox.Focus();
        }
  • what exactly doesnt work? – casiosmu May 02 '18 at 14:21
  • Can you clarify what doesn't work? Since you're using PreviewMouseUp and PreviewMouseDown event handlers, a standard click (quick down and up on the mouse) would immediately hide then show the corresponding boxes. If you intend for the user to click and hold the eye button, maybe that's what you want? It's just not clear what you're expecting. – Sean Beanland May 02 '18 at 14:24
  • When I say "it doesn't work" I mean that I still see dots and can't see the real password –  May 02 '18 at 14:28
  • 1
    it looks like you did it the opposite way when you mouse down you make the passwordbox visible – Shloime Rosenblum May 02 '18 at 15:26
  • There are several approaches, here's a trick, you could use FontFamily="Wingdings 2" in textbox and change to FontFamily="Tahoma" at button click – Clint May 02 '18 at 21:33

0 Answers0