Marlon Brando aka Ben
Marlon Brando aka Ben

Reputation: 933

How to only allow '0's and '1's to be put into a textbox in windows phone

In my windows app there is a textfield to input numbers(binary 1 or 0) for users. I'm building this app to convert binary numbers into decimal. Now What I want is , when user enter a binary number in textfield, other numbers should disable, or should give an error message. help me with this. thanx.!!!!

Upvotes: 0

Views: 116

Answers (2)

Vyas
Vyas

Reputation: 2774

For Windows Phone 8 - Silverlight Application.

Hook to your Textbox's KeyDown Event;

private void yourTextbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
     // Check for 1 & 0
     if ((e.PlatformKeyCode != 48) || (e.PlatformKeyCode != 49) || (e.PlatformKeyCode != 8))
     { e.Handled = true; }
}

Don't forget to set you Textbox's InputScope="Number" in XAML.

Upvotes: 1

Igor Kulman
Igor Kulman

Reputation: 16361

Hook into the KeyUp event of the TextBox and check if the key is 0 or 1. If not, simply delete it.

Upvotes: 1

Related Questions