1

I used Visual Studio to create an application that features 3 fields where 3 numbers can be added together. Now, I need to validate all 3 fields so that negative numbers cannot be added. If a negative number is entered, each field has also to return a unique message like: "please enter a positive first number."

I figured out how to do that for one field, but how do I set it up for all 3 fields to not accept negative numbers (and display a unique message)?

Here is what I have:

{
    int num = int.Parse(txtNum1.Text);

    if (num <0)
    {
        MessageBox.Show("Please enter a positive first number");
    }
    else
    {
        int num1 = int.Parse(txtNum1.Text);
        int num2 = int.Parse(txtNum2.Text);
        int num3 = int.Parse(txtNum3.Text);
        int sum = num1 + num2 + num3;
        txtResult.Text = sum.ToString();
    }

Hopefully this makes sense.

1
  • 1
    "Hopefully this makes sense." -- sorry, no. Not to me it doesn't. It's not even clear what GUI API you're using here. But all of the APIs (e.g. Winforms, WPF, Winrt) have built-in field validation features, which you should be using. Beyond that, your code already parses all three fields. Why can't you follow that example and check each parsed value to make sure it's non-negative (note that strictly speaking, that's what you're doing with the first...it can be zero, instead of being positive)? It's not clear at all what you need help with, and there's not enough context here. Commented Oct 20, 2016 at 1:31

2 Answers 2

1

This might help you

int num = 0;
bool atLeastOneisNegative = false;
foreach (Control x in this.Controls)
{
    if (x is TextBox)
    {
        num = 0;
        num = int.Parse(((TextBox)x).Text);
        if(num < 0)
        {
            atLeastOneisNegative = true;
            MessageBox.Show("Please enter a positive first number");
        }
    }
}   

if(!atLeastOneisNegative)
{
    int num1 = int.Parse(txtNum1.Text);
    int num2 = int.Parse(txtNum2.Text);
    int num3 = int.Parse(txtNum3.Text);
    int sum = num1 + num2 + num3;
    txtResult.Text = sum.ToString();
}

Whereas the question is not stating whether you are using WPF or WinForms. But the logic might help you to achieve to iterate all your TextBoxes and see the value is Positive.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks everyone for you input. This is only my second week in a C# class. I figured out the addition thing. Now I'm trying to do the same thing for multiplication. None of the inputted numbers can be zero. For some reason I'm having trouble. I "cannot implicitly convert type "int" to "bool."
Hi @Ulcis if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
Secondly if you have a different question create a new question with all the details like what your form looks like. what are the validations? what is the code you have tried. What is the output (error/logical error) and what is expected output.
0

It can be easier with NumericUpDown control or handling the TextBox.Validating event, but anyway:

int i1, i2, i3;
if (!int.TryParse(txtNum1.Text, out i1) || i1 < 0) { MessageBox.Show("Please enter a positive first number" ); return; }
if (!int.TryParse(txtNum2.Text, out i2) || i2 < 0) { MessageBox.Show("Please enter a positive second number"); return; }
if (!int.TryParse(txtNum3.Text, out i3) || i3 < 0) { MessageBox.Show("Please enter a positive third number" ); return; }
int sum = i1 + i2 + i3;
txtResult.Text = sum.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.