I have a simple keypad with 0-9, a Clear button, and an Enter Button. Clicking the numbers places the content into a PasswordBox. Clicking the Clear button simulates a backspace, and the Enter Button acts as a submit for predefined 4 digit codes. If the correct code is entered, it appends to a textbox showing that access was granted, else, access denied.
I am having no problems except when trying to set a MaxLength on the PasswordBox. I have tried it in my XAML with:
<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"/>
I've also tried it programmatically with:
securityCodeTextBox.MaxLength = 4;
This is the only code in my function for the numeric buttons:
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
securityCodeTextBox.Password += button.Content.ToString();
}
I don't have to implement this, since if the code is wrong, it will just log "Access Denied" in the textbox below it. However, I am really curious on how to do this after realizing the MaxLength is for keyboard input, not button clicks. I currently have the keyboard completely disabled with:
//Prevent keyboard input
private void securityCodeTextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Next attempt
I created a very sloppy way to disable all but the Clear and Enter buttons if the length reaches >=4 with these code snippets in the Num and Clear Button Click functions:
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;
}
}
Clear Button
/**
* 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;
}
}
Is there a cleaner way to implement the MaxLength method when the input is the content of a button?
I never worked with WPF or password input Controls before, but I would try it like this, it just wont add up the password if the length is reached.
private void ButtonClickNum(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
if(securityCodeTextBox.Password.Length < 4)
securityCodeTextBox.Password += button.Content.ToString();
}