Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I only accept Numeric Digits in C# Xamarin

Tags:

c#

ios

xamarin

I'm New to C# with Xamarin. Im using Visual Studio for Mac Preview to work on an iOS app. When Making a basic calculator I can't figure out how to not accept answers that aren't a numeric digit. For example in my Text box if I put in a letter I want an error that says

"Please enter a numeric digit".

I don't know why I'm getting an error but its not working.

When I run it and put in a number it just defaults to the else statement every time. Also with Letters. And when I put too many letters in 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";
        }
    }   
like image 765
Kyle Avatar asked May 01 '17 01:05

Kyle


People also ask

Is digit function in C?

The isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it's a digit else it returns 0. For example, it returns a non-zero value for '0' to '9' and zero for others. The isdigit() is declared inside ctype.

What is numeric character in C?

C isdigit() The isdigit() function checks whether a character is numeric character (0-9) or not.

How do you input only numbers in C++?

Program for taking only integer input in C++ When the number of characters is unknown, we use numeric_limits<streamsize>::max() to ignore upto the maximum length of any string passed. The delimiter I passed here is '\n' (the second parameter).

Which C function is used to get the number from keyboard?

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.

How do you find the digit of a number in C?

The function getdigit() uses two C integer operator, modulo and div, to find the digit of a particular location of a number. It first does the modulo operation on the input number by 10, if position is 0, 100 if position is 1, 1000 if position is 2 and so on.

How to create a text box that only accepts numbers in C #?

This tutorial will introduce the methods to create a text box that only accepts numbers in C#. The NumberUpDown view is used to get numeric input from the user in C#. To add a NumberUpDown view in our Windows Form application, we just select NumberUpDown from the toolbox and drag it to our form.

How do I create an input that only accepts numbers?

If you want to create an input that only accepts number, the first thing that you need to think in is the NumericUpDown control. This controls represents a Windows spin box (also known as an up-down control) that displays exclusively numeric values.

How to prevent input from non numeric characters inside a textbox?

Learn how to prevent the input from non numeric characters inside a textbox in Winforms. To create an input element on your Windows Form that only accepts numbers, you have 2 options: If you want to create an input that only accepts number, the first thing that you need to think in is the NumericUpDown control.


2 Answers

I think the problem is string input = Console.ReadLine(); Why are you expecting a correct value with that statement? That would be valid for a console program but you are running an iOS app.

It would make sense something like:

string input = YourTextInput.Text;

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

YourTextInput.KeyboardType = UIKeyboardType.NumberPad;

And one more thing. If the answer should be a double, you should use double.TryParse(input, out value) instead of int.

What 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 validate the number entered, as the number pad allows to type more than one decimal point.

like image 146
xleon Avatar answered Oct 11 '22 02:10

xleon


I'd recommend changing the TextBox so the keyboard which appears is numeric only.

<Entry Keyboard="Numeric" />

Check out this Xamarin Guide for more info.

like image 3
Troy Poulter Avatar answered Oct 11 '22 01:10

Troy Poulter