当输入为 Button.Content 而不是键盘时,如何在 PasswordBox 中设置 MaxLength? (WPF)
How to set MaxLength in PasswordBox when input is Button.Content instead of keyboard? (WPF)
我有一个带有 0-9 的简单键盘、一个清除按钮和一个输入按钮。单击数字会将内容放入 PasswordBox。单击 Clear 按钮模拟退格,Enter 按钮用作提交预定义的 4 位代码。如果输入了正确的代码,它会附加到一个文本框,显示访问已被授予,否则,访问被拒绝。
除了尝试在 PasswordBox 上设置 MaxLength 时,我没有遇到任何问题。我已经在我的 XAML 中尝试过:
<PasswordBox PasswordChanged="securityCodeTextBox_PasswordChanged" MaxLength="4" KeyDown="securityCodeTextBox_KeyDown" x:Name="securityCodeTextBox" PasswordChar="•" HorizontalAlignment="Left" Margin="117,20,0,0" VerticalAlignment="Top" Height="23" Width="213"/>
我也以编程方式尝试过:
securityCodeTextBox.MaxLength = 4;
这是我的函数中唯一用于数字按钮的代码:
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
securityCodeTextBox.Password += button.Content.ToString();
}
我不必执行此操作,因为如果代码有误,它只会在其下方的文本框中记录 "Access Denied"。但是,在意识到 MaxLength 是针对 键盘输入 而不是按钮点击之后,我真的很好奇如何做到这一点。我目前完全禁用了键盘:
//Prevent keyboard input
private void securityCodeTextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
下次尝试
如果长度达到 >=4,我创建了一种非常草率的方法来禁用除清除和输入按钮之外的所有按钮,这些代码片段位于 Num 和 Clear Button Click 函数中:
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
securityCodeTextBox.Password += button.Content.ToString();
//Disable all buttons if MaxLength is reached
if(securityCodeTextBox.Password.Length >= 4)
{
button0.IsEnabled = false;
button1.IsEnabled = false;
button2.IsEnabled = false;
button3.IsEnabled = false;
button4.IsEnabled = false;
button5.IsEnabled = false;
button6.IsEnabled = false;
button7.IsEnabled = false;
button8.IsEnabled = false;
button9.IsEnabled = false;
}
}
清除按钮
/**
* Remove(int startIndex, int count) startIndex = position, count = num of chars to delete
*/
private void ButtonClickClear(object sender, RoutedEventArgs e)
{
if (securityCodeTextBox.Password.Length > 0)
{
securityCodeTextBox.Password = securityCodeTextBox.Password.Remove(securityCodeTextBox.Password.Length - 1, 1);
}
//Enable all the buttons again once password is less than MaxLength
if (securityCodeTextBox.Password.Length < 4)
{
button0.IsEnabled = true;
button1.IsEnabled = true;
button2.IsEnabled = true;
button3.IsEnabled = true;
button4.IsEnabled = true;
button5.IsEnabled = true;
button6.IsEnabled = true;
button7.IsEnabled = true;
button8.IsEnabled = true;
button9.IsEnabled = true;
}
}
当输入是按钮的内容时,是否有更简洁的方法来实现 MaxLength 方法?
我以前从未使用过 WPF 或密码输入控件,但我会这样尝试,如果达到长度,它不会添加密码。
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
if(securityCodeTextBox.Password.Length < 4)
securityCodeTextBox.Password += button.Content.ToString();
}
我有一个带有 0-9 的简单键盘、一个清除按钮和一个输入按钮。单击数字会将内容放入 PasswordBox。单击 Clear 按钮模拟退格,Enter 按钮用作提交预定义的 4 位代码。如果输入了正确的代码,它会附加到一个文本框,显示访问已被授予,否则,访问被拒绝。
除了尝试在 PasswordBox 上设置 MaxLength 时,我没有遇到任何问题。我已经在我的 XAML 中尝试过:
<PasswordBox PasswordChanged="securityCodeTextBox_PasswordChanged" MaxLength="4" KeyDown="securityCodeTextBox_KeyDown" x:Name="securityCodeTextBox" PasswordChar="•" HorizontalAlignment="Left" Margin="117,20,0,0" VerticalAlignment="Top" Height="23" Width="213"/>
我也以编程方式尝试过:
securityCodeTextBox.MaxLength = 4;
这是我的函数中唯一用于数字按钮的代码:
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
securityCodeTextBox.Password += button.Content.ToString();
}
我不必执行此操作,因为如果代码有误,它只会在其下方的文本框中记录 "Access Denied"。但是,在意识到 MaxLength 是针对 键盘输入 而不是按钮点击之后,我真的很好奇如何做到这一点。我目前完全禁用了键盘:
//Prevent keyboard input
private void securityCodeTextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
下次尝试
如果长度达到 >=4,我创建了一种非常草率的方法来禁用除清除和输入按钮之外的所有按钮,这些代码片段位于 Num 和 Clear Button Click 函数中:
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
securityCodeTextBox.Password += button.Content.ToString();
//Disable all buttons if MaxLength is reached
if(securityCodeTextBox.Password.Length >= 4)
{
button0.IsEnabled = false;
button1.IsEnabled = false;
button2.IsEnabled = false;
button3.IsEnabled = false;
button4.IsEnabled = false;
button5.IsEnabled = false;
button6.IsEnabled = false;
button7.IsEnabled = false;
button8.IsEnabled = false;
button9.IsEnabled = false;
}
}
清除按钮
/**
* Remove(int startIndex, int count) startIndex = position, count = num of chars to delete
*/
private void ButtonClickClear(object sender, RoutedEventArgs e)
{
if (securityCodeTextBox.Password.Length > 0)
{
securityCodeTextBox.Password = securityCodeTextBox.Password.Remove(securityCodeTextBox.Password.Length - 1, 1);
}
//Enable all the buttons again once password is less than MaxLength
if (securityCodeTextBox.Password.Length < 4)
{
button0.IsEnabled = true;
button1.IsEnabled = true;
button2.IsEnabled = true;
button3.IsEnabled = true;
button4.IsEnabled = true;
button5.IsEnabled = true;
button6.IsEnabled = true;
button7.IsEnabled = true;
button8.IsEnabled = true;
button9.IsEnabled = true;
}
}
当输入是按钮的内容时,是否有更简洁的方法来实现 MaxLength 方法?
我以前从未使用过 WPF 或密码输入控件,但我会这样尝试,如果达到长度,它不会添加密码。
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
if(securityCodeTextBox.Password.Length < 4)
securityCodeTextBox.Password += button.Content.ToString();
}