pariwesh07 pariwesh07 - 1 month ago 9
C# Question

Trying to hide password on image click

Trying to hide password while clicking on the image, The code are provided below.In the xaml part i have taken textbox for accepting password in the code behind i had written the code for hiding but it is giving error while making visibility false.
Refer imageenter image description here

<TextBox Name="txtBoxPassw" Height="45" Width="246" InputScope="Password" Margin="10,5,5,5" Background="White" VerticalAlignment="Center"
FontSize="25" FontFamily="Segoe UI Light" Visibility="Visible" Foreground="Black" Padding="50,5,5,5" TextWrapping="Wrap"
BorderThickness="0,0,0,2" BorderBrush="Gray" />
<Canvas Margin="58,-45,136,0">
<Image x:Name="passLogo" Source="Assets/ic_pass.png" Height="41" Width="41" />
</Canvas>
<!--19oct-->
<TextBlock Name="txtBlockPasswFieldError" Grid.Row="2" Margin="-60,0,0,0" Foreground="Black" Text="*Please enter valid name"
HorizontalAlignment="Center" Visibility="Visible" FontSize="11"/>
<!--19oct-->
<Image Name="showimg" Source="Assets/show_pass.png" Width="25" Height="50" Margin="50,15,40,10" Tapped="Image_Tapped" Stretch="Uniform"/>
<TextBlock Name="showPass"
Text="Show Password"
Foreground="#303030"
FontSize="15"
FontFamily="Koblenz Serial Medium"
Margin="200,-45,15,20" />


cs file

private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{

BitmapImage bitcurrentimage = showimg.Source as BitmapImage;
//if (showimg.Source == new BitmapImage(new Uri("ms-appx:///Assets/show_pass.png", UriKind.RelativeOrAbsolute)))
if (bitcurrentimage.UriSource.AbsoluteUri == "ms-appx:///Assets/show_pass.png")
{
//passBox.PasswordRevealMode = PasswordRevealMode.Visible;
txtBoxPassw.Visibility = Visibility.Visible;
showPass.Text = "Hide Password";
SetImage("ms-appx:///Assets/hide_pass_.png");
}
else
{
//passBox.PasswordRevealMode = PasswordRevealMode.Hidden;
txtBoxPassw.Visibility = Visibility.Visible.Equals(false);
showPass.Text = "Show Password";
SetImage("ms-appx:///Assets/show_pass.png");
}

}

Answer

actually i want to put validation(if (Utility.isEmptyString(txtBoxPassw.Text)) on the string typed in passwordbox, but when i am trying it is giving error "'PasswordBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'PasswordBox' could be found (are you missing a using directive or an assembly reference?)"

This is your last comment, and from this comment I assume that you accept the method to use the built-in control PasswordBox instead of creating your own one.

Then you can refer to PasswordBox class, unlike a TextBox or a TextBlock, PasswordBox does not have a property named "Text". I think what you need is the PasswordBox.Password property.

For example:

<PasswordBox x:Name="passwb" Height="30" IsPasswordRevealButtonEnabled="True" />
<Button Content="click" VerticalAlignment="Bottom" Click="Button_Click"  />

Code behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var pass = passwb.Password;
}

The pass here indicate the password currently held by the PasswordBox which named "passwb".

Comments