Help to know what is wrong

Asked

Viewed 76 times

-1

//Função para permitir somente números e ponto
private void ApenasValorNumerico(object sender, KeyEventArgs e)
{
    TextBox txt = (TextBox)sender;
    if (e.Key != Key.Back)
    {
        if (Convert.ToChar(e.Key) == '.')
            e.Handled = (txt.Text.Contains('.'));
        else
            e.Handled = true;
    }
}

Well, I found this code in another post, and modified it to try to use in my application for Windowsphone, but I don’t know where the error is.. I am a beginner. Thanks in advance for any help..

PS.: Actually no error occurs, it just doesn’t work.. the method is called but it doesn’t allow inclusion in the textbox.. none of the "keys" work, nor numbers, nor point.

  • 2

    What error occurs?

  • Detail plus the question, report the error that is happening, any additional information you find later should be added in the question itself, not in an answer. And don’t forget to mark the answer that best helped you/solved the case.

  • Actually no error occurs, it simply does not work.. the method is called but it does not allow inclusion in the textbox.. none of the "keys" work, nor numbers, nor point..

  • This appears to be the original: http://answall.com/questions/28129/stilo-moeda-numa-textbox-em-winforms/28192#28192

  • @Yes, this is the original..

1 answer

2


You can make the following comparison:

  if (System.Text.RegularExpressions.Regex.IsMatch(e.Key.ToString(),"[0-9]|."))
   {
      e.Handled = false;
   }
   else
   {
       else e.Handled = true;
   }
  • What is that 8 in if?

  • 1

    For Textbox to accept Backspace

  • I added the point validation, but I suggest you create a mask for Textbox

  • I am not the OP of the question :p

  • I know, but I thought it would be useful to complete the answer also for the author of the question

  • @Ari This worked, Backspace and numbers, but not the point.. Anyway I’m already grateful..

  • @7..BK, test with the following comparison: if (System.Text.Regularexpressions.Regex.Ismatch(e.Key.Tostring(),"[0-9]|.")

  • @Ari, it worked, but not like this ,"[0-9]|.". I removed the backslash, "[0-9]|." and it worked.. Thank you very much..

  • I edited the answer to make it complete

  • @7..BK, I believe I am a better retest, because in REGEX the . means anything, is sure that it is validating only numbers and the point?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.