繁体   English   中英

当输入是Button.Content而不是键盘时,如何在PasswordBox中设置MaxLength? (WPF)

[英]How to set MaxLength in PasswordBox when input is Button.Content instead of keyboard? (WPF)

我有一个带有0-9的简单键盘,一个清除按钮和一个输入按钮。 单击数字将内容放入PasswordBox。 单击“清除”按钮将模拟退格,并且“输入”按钮充当预定义4位数字代码的提交。 如果输入正确的代码,它将附加到一个文本框,显示已授予访问权限,否则,访问被拒绝。

WPF应用程序

除了尝试在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();
    }

我不必执行此操作,因为如果代码错误,它将仅在其下面的文本框中记录“访问被拒绝”。 但是,我对实现MaxLength是用于键盘输入而不是按钮单击之后的操作感到非常好奇。 我目前使用以下命令完全禁用了键盘:

//Prevent keyboard input

    private void securityCodeTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
    }

下次尝试

我创建了一个非常草率的方法,如果长度达到> = 4,则禁用除“清除”和“输入”按钮之外的所有按钮,并在Num和“清除按钮的单击”功能中使用以下代码段:

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();             
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM