gpt4 book ai didi

c# - 当我单击 CheckBox 时,Wpf PasswordBox 必须显示字符

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:40 28 4
gpt4 key购买 nike

xaml

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel VerticalAlignment="Center" Width="300">
<PasswordBox x:Name="PasswordBox1" Height="30" PasswordChar="*" Password="12345"/>
<CheckBox x:Name="CheckBox1" Content="Show Password"/>
</StackPanel>
</Window>

vb.net

Class MainWindow 
Private Sub CheckBox1_Checked(sender As Object, e As RoutedEventArgs) Handles CheckBox1.Checked
PasswordBox1.PasswordChar = CChar("")
End Sub
Private Sub CheckBox1_Unchecked(sender As Object, e As RoutedEventArgs) Handles CheckBox1.Unchecked
PasswordBox1.PasswordChar = CChar("*")
End Sub
End Class

运行以上代码并单击 CheckBox1 以了解发生了什么。

当我点击 CheckBox 时,PasswordBox 如何显示 12345 的字符?

因此,需要修复以下行。

PasswordBox1.PasswordChar = CChar(" ")

最佳答案

这将适用于您正在寻找的内容,尽管它会在内存中暴露您的密码。我们在 UI 的同一位置有一个文本框和一个密码框,当用户选中“显示密码”复选框时,我们折叠密码框并显示隐藏的文本框,同时更新文本。发送密码时,您需要检查您是否使用可见 UI 控件中的密码。

Xaml 代码:

<StackPanel Orientation="Horizontal">
<Grid Width="300" Height="40">
<PasswordBox Name="passwordBox" PasswordChar="*" />
<TextBox Name="passwordTxtBox" Visibility="Collapsed" />
</Grid>
<CheckBox Margin="10" Name="showPassword" Unchecked="ShowPassword_Unchecked" Checked="ShowPassword_Checked" />
</StackPanel>

代码隐藏:

    private void ShowPassword_Checked(object sender, RoutedEventArgs e)
{
passwordTxtBox.Text = passwordBox.Password;
passwordBox.Visibility = Visibility.Collapsed;
passwordTxtBox.Visibility = Visibility.Visible;
}

private void ShowPassword_Unchecked(object sender, RoutedEventArgs e)
{
passwordBox.Password = passwordTxtBox.Text;
passwordTxtBox.Visibility = Visibility.Collapsed;
passwordBox.Visibility = Visibility.Visible;
}

关于c# - 当我单击 CheckBox 时,Wpf PasswordBox 必须显示字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50650292/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com