DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Culture and Methodologies
  3. Agile
  4. PIN Number Password Fields for Windows XAML

PIN Number Password Fields for Windows XAML

How to get around some limitations in XAML definitions for your user interface.

Has Altaiar user avatar by
Has Altaiar
·
Oct. 28, 15 · Tutorial
Like (2)
Save
Tweet
Share
3.58K Views

Join the DZone community and get the full member experience.

Join For Free

In the Windows XAML world, a bad design decision was made to have the passwordBox and the TextBox different, 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 like customizing the appearance of the password textbox (say you want to make the text center-aligned, or you want to bind the number key pad instead of the alpha keyboard). I had this exact requirement 2 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 a 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:

<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" />


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

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


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, then 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 RegEx to mask the display of the PIN on the screen. Also, notice how we listen to back key press to clear the last char.

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


Event code style Requirement Alpha (finance) Property (programming) Design Snippet (programming) Clear (Unix) Binding (linguistics) Blog

Published at DZone with permission of Has Altaiar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • What Is Testing as a Service?
  • Apache Kafka Introduction, Installation, and Implementation Using .NET Core 6
  • TDD: From Katas to Production Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: