Monday, October 29, 2012

textBox refocus if input invalid

textBox refocus if input invalid

I am a slow learner when it comes to programming, sorry. I am doing a windows phone application in silverlight. I am trying to validate input.

The problem I have is that I am detecting the bad input and the message comes up, but the keyboard dissappears, effectively allowing them to go on with the input error still there. So is there a way to stop them from leaving without having valid input?

Thankyou

bool IsDigitsOnly(string str) {         foreach (char c in str) {             if (c < '0' || c > '9') {                  return false;              }         }          return true;     }      protected Boolean noLetters() {            if (!IsDigitsOnly(ReadySecTxtBox.Text)) {             MessageBox.Show("digits only");             ReadySecTxtBox.Focus();             return false;         }         if (!IsDigitsOnly(RoundSecTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(RestSecTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(RelaxSecTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(ReadyMinTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(RoundMinTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(RestMinTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(RelaxMinTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         if (!IsDigitsOnly(NoRoundsTxtBox.Text)) {             MessageBox.Show("digits only");             return false;         }         else return true;      }      protected Boolean validInputs(){           if (noLetters()) {               if (int.Parse(ReadySecTxtBox.Text) < 0 || int.Parse(ReadySecTxtBox.Text) > 59) {                 MessageBox.Show("seconds must be between 0 and 59");                  return false;             }             if (int.Parse(RoundSecTxtBox.Text) < 0 || int.Parse(RoundSecTxtBox.Text) > 59) {                 MessageBox.Show("seconds must be between 0 and 59");                 return false;             }             if (int.Parse(RestSecTxtBox.Text) < 0 || int.Parse(RestSecTxtBox.Text) > 59) {                 MessageBox.Show("seconds must be between 0 and 59");                 return false;             }             if (int.Parse(RelaxSecTxtBox.Text) < 0 || int.Parse(RelaxSecTxtBox.Text) > 59) {                 MessageBox.Show("seconds must be between 0 and 59");                 return false;             }              if (int.Parse(ReadyMinTxtBox.Text) < 0 || int.Parse(ReadyMinTxtBox.Text) > 99) {                 MessageBox.Show("minutes must be between 0 and 99");                 return false;             }             if (int.Parse(RoundMinTxtBox.Text) < 0 || int.Parse(RoundMinTxtBox.Text) > 99) {                 MessageBox.Show("minutes must be between 0 and 99");                 return false;             }             if (int.Parse(RestMinTxtBox.Text) < 0 || int.Parse(RestMinTxtBox.Text) > 99) {                 MessageBox.Show("minutes must be between 0 and 99");                 return false;             }             if (int.Parse(RelaxMinTxtBox.Text) < 0 || int.Parse(RelaxMinTxtBox.Text) > 99) {                 MessageBox.Show("minutes must be between 0 and 99");                 return false;             }              if (int.Parse(NoRoundsTxtBox.Text) < 1 || int.Parse(NoRoundsTxtBox.Text) > 99) {                 MessageBox.Show("number of rounds must be between 1 and 99");                 return false;             }               else return true;         }         else return false;     }      protected override void OnManipulationStarted(ManipulationStartedEventArgs e) {          if (validInputs()) {               totalSec = (int.Parse(RoundSecTxtBox.Text) + int.Parse(RestSecTxtBox.Text)) * int.Parse(NoRoundsTxtBox.Text)                 + int.Parse(RelaxSecTxtBox.Text) + int.Parse(ReadySecTxtBox.Text);             totalMin = (int.Parse(RoundMinTxtBox.Text) + int.Parse(RestMinTxtBox.Text)) * int.Parse(NoRoundsTxtBox.Text)                 + int.Parse(RelaxMinTxtBox.Text) + int.Parse(ReadyMinTxtBox.Text);              TotalMinTxtBox.Text = totalMin.ToString("00");             TotalSecTxtBox.Text = totalSec.ToString("00");         }          base.OnManipulationStarted(e);       } 

Answers & Comments...

Answer: 1

Use this piece of code snippet:

private bool Validate(string text) {     if (!IsDigitsOnly(text)) {         MessageBox.Show("digits only");                     return false;     } }  protected Boolean noLetters() {      if (!Validate(ReadySecTxtBox.Text))     {         ReadySecTxtBox.Focus();         return false;     }      if (!Validate(RoundSecTxtBox.Text))     {         RoundSecTxtBox.Focus();         return false;     }      //...      else return true; } 

ADDED: Or you can do it this way.

private bool ValidateAndFocus(ref TextBox txt) {     if (!IsDigitsOnly(txt.Text)) {         MessageBox.Show("digits only");                     txt.Focus();         return false;     } }  protected Boolean noLetters() {      if (!ValidateAndFocus(ref ReadySecTxtBox))         return false;      if (!ValidateAndFocus(ref RoundSecTxtBox))         return false;      //...      else return true; } 
by : Furqan Safdarhttp://stackoverflow.com/users/387058




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog