Skip to content

Instantly share code, notes, and snippets.

@wirasetiawan29
Created October 8, 2014 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wirasetiawan29/67f40172fec559a85233 to your computer and use it in GitHub Desktop.
Save wirasetiawan29/67f40172fec559a85233 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using input_password.Resources;
using System.Windows.Input;
using System.Text.RegularExpressions;
namespace input_password
{
public partial class MainPage : PhoneApplicationPage
{
string _enteredPasscode = "";
string _passwordChar = "*";
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PasswordTextBox_KeyUp(object sender, KeyEventArgs e)
{
//modify new passcode according to entered key
_enteredPasscode = GetNewPasscode(_enteredPasscode, e);
ActualPasscode.Text = _enteredPasscode;
//replace text by *
PasswordTextBox.Text = Regex.Replace(_enteredPasscode, @".", _passwordChar);
//take cursor to end of string
PasswordTextBox.SelectionStart = PasswordTextBox.Text.Length;
}
private string GetNewPasscode(string oldPasscode, KeyEventArgs keyEventArgs)
{
string newPasscode = string.Empty;
switch (keyEventArgs.Key)
{
case Key.D0:
case Key.D1:
case Key.D2:
case Key.D3:
case Key.D4:
case Key.D5:
case Key.D6:
case Key.D7:
case Key.D8:
case Key.D9:
newPasscode = oldPasscode + (keyEventArgs.PlatformKeyCode - 48);
break;
case Key.Back:
if (oldPasscode.Length > 0)
newPasscode = oldPasscode.Substring(0, oldPasscode.Length - 1);
break;
default:
//others
newPasscode = oldPasscode;
break;
}
return newPasscode;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment