How to accept only numeric digits in C # Xamarin

I am new to C # with Xamarin. I am using Visual Studio for Mac Preview to work in an iOS app. While building a basic calculator, I cannot figure out how not to accept answers that are not a numerical digit. For example, in my text box, if I have pasted an email, I want to get the error

"Enter a numeric digit."

I don't know why I am getting the error, but not working.

When I run it and put in a number, it just references the else statement by default. Also with Letters. And when I place too many letters, it crashes.

    partial void AddButton_TouchUpInside(UIButton sender)
    {
        double number1 = 0.00;
        double answer = 0.00;
        double number2 = 0.00;

        int value;
        string input = Console.ReadLine();
        if (Int32.TryParse(input, out value))
        {
            number1 = double.Parse(Number1TextBox.Text);
            number2 = double.Parse(Number2TextBox.Text);
            answer = number1 + number2;
            AnswerLabel.Text = "The answer is: " + answer.ToString();                             
        }
        else
        {

            InvalidLabel.Text = "Please enter a Numeric Digit";
        }
    }   

      

+3


source to share


3 answers


I think the problem is string input = Console.ReadLine();

Why are you expecting the correct value with this expression? This is applicable for a console program, but you are using an iOS app.

It will make sense:

string input = YourTextInput.Text;

      

If you want to restrict keyboard text input to numbers, use the following:

YourTextInput.KeyboardType = UIKeyboardType.NumberPad;

      



One more thing. If the answer should be double

, you should use double.TryParse(input, out value)

int instead.

How about this?

double number1 = 0.00;
double answer = 0.00;
double number2 = 0.00;

if (double.TryParse(Number1TextBox.Text, out number1)
    && double.TryParse(Number2TextBox.Text, out number2))
{
    answer = number1 + number2;
    AnswerLabel.Text = "The answer is: " + answer.ToString();                             
}
else
{
    InvalidLabel.Text = "Please enter a Numeric Digit";
}

      

You still need to check the number entered as the numeric keypad allows you to enter more than one decimal point.

+1


source


I would recommend changing the TextBox so that the keyboard that appears is only numeric.

<Entry Keyboard="Numeric" />

      



Check out this Xamarin Guide for more information.

+2


source


Okay, I'm still a little confused. I've added all the code you said and when I enter a letter or two. The app just crashes with an error. So it still doesn't work. Also I tried to include under string input, .Text with the stuff you said and it just gave me a red line and won't work. Sorry, it just can't figure it out. Thanks to

Here's my code

    partial void AddButton_TouchUpInside(UIButton sender)
    {
        double number1 = 0.00;
        double answer = 0.00;
        double number2 = 0.00;
        string input = Number1TextBox.Text;
        string inout = Number2TextBox.Text;

        if (double.TryParse(Number1TextBox.Text, out number1)
            && double.TryParse(Number2TextBox.Text, out number2))

        {
            answer = number1 + number2;

            AnswerLabel.Text = "The answer is: " + answer.ToString();
        }
        else
        {
            InvalidLabel.Text = "Please enter a Numeric Digit";
        }




    }

      

0


source







All Articles