In Windows XAML world, a bad design decision was made to have PasswordBox and TextBox as different objects, meaning that the PasswordBox does not inherit common properties from the TextBox. As a consequence, you cannot do many of the things that you normally do with a textbox such as customising the appearance of the password textbox (say you want to make the text centre-aligned, or you want to bind the number keypad instead of the alpha keyboard). I had this exact requirement two weeks ago and I had to solve it, so this blog talks about the approach I took to make this happen.

Basically all I needed to do was to switch from using password boxes to normal textbox and then wire the key up event in the background to hide the entered password. You need to be careful though when handling these events, because you could end up making your text fields unusable. In my scenario, I needed the textbox to act like a PIN number (Password) field, so I only accepted numbers as you can see in the code below. Here is the code snippet:

[code lang=”xml”]
<TextBox x:Name="PinNumberTextBox" HorizontalAlignment="Center"
Margin="0,200,0,0" VerticalAlignment="Top" MinWidth="300"
PlaceholderText="Enter 4-8 digits PIN no"
InputScope="Number" KeyUp="PinNumberTextBox_KeyUp"
TextAlignment="Center" />
[/code]

As you can see above, I am using a TextBox in the XAML for the PIN number and using normal TextAlignment to centre the text and InputScope for binding the number keypad instead of the default alpha keyboard. Also, notice that I am wiring the KeyUp event. Here is the code of the keyUp event.

[code lang=”csharp”]
string _enteredPin = "";
string _confirmPin = "";
string _passwordChar = "*";

private void PasswordTextBox_KeyUp(object sender, KeyRoutedEventArgs e, TextBox field, ref string pinCode)
{
 //modify new passcode according to entered key
 pinCode = GetNewPasscode(ref pinCode, e);
 
 //replace text by *
 field.Text = Regex.Replace(pinCode, @".", _passwordChar);

//take cursor to end of string
 field.SelectionStart = field.Text.Length;

// stop the event from propagating further
e.Handled = true;
}

private string GetNewPasscode(ref string oldPasscode, KeyRoutedEventArgs keyEventArgs)
{
 string newPasscode = string.Empty;

switch (keyEventArgs.Key)
 {
 case VirtualKey.Number0:
case VirtualKey.Number1:
 case VirtualKey.Number2:
 case VirtualKey.Number3:
 case VirtualKey.Number4:
 case VirtualKey.Number5:
 case VirtualKey.Number6:
 case VirtualKey.Number7:
 case VirtualKey.Number8:
 case VirtualKey.Number9:

var numKey = keyEventArgs.Key.ToString();
 var number = numKey.Substring(numKey.Length – 1, 1);
 newPasscode = oldPasscode + number; 
 break;

case VirtualKey.Back:

if (oldPasscode.Length > 0)
 newPasscode = oldPasscode.Substring(0, oldPasscode.Length – 1);

break;

default:
 //others

newPasscode = oldPasscode;
 break;
 }
return newPasscode;
 }
[/code]

Because I had two Textboxes (PinNumberTextBox and ConfirmPinNumberTextBox), I have the handling of the event in a method called GetNewPasscode() and that takes the relevant text box and updates the pinNumber (entered value).

As you can see, it is fairly simple – we listen to keys when the user types something, and we only accept numbers (this could be changed based on your requirements) and update the enteredPin that we hold in an instance property. We then use a regular expression to mask the display of the PIN on the screen. Finally, notice how we listen to back key press to clear the last character in that case.

Hope this helps someone, and please do get in touch if you have any comments or questions.

Category:
Application Development and Integration, Mobile
Tags:
, , ,