none
IF-Statements? RRS feed

  • Question

  • So I'm trying to figure out how do I create an if-statement in windows for? I'm wanting to write something like

    textBox3.Text = (Double.Parse(textBox1.Text) - Double.Parse(textBox1.Text)).ToString();
                if(textBox3.Text > 0)
                {
                    MessageBox.Show("Your answer is {0}:", (Double.Parse(textBox1.Text) - Double.Parse(textBox1.Text)).ToString());
                }
               

    -------------------------------------------------------------------------------------------------------------------------

    So basically I'm trying to make it to where if the answer is greater than 0 I want it to return it in a Message Box, How do I do this?

    Sunday, March 12, 2017 8:17 PM

Answers

  • Hi Mweyola777,

    Thank you for posting here.

    For your question, you could try the following code.

        private void button1_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
                {
                    textBox3.Text = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text)).ToString();
                }
                if (Convert.ToInt32(textBox3.Text) > 0)
                {
                    MessageBox.Show("Your answer is " + textBox3.Text);
                }
            }

    I use a gif to show the result.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 13, 2017 2:27 AM
    Moderator

All replies

  • If you want to convert TextBox to integer to make a comparison :

    How to: Convert the Text in a TextBox Control to an Integer

    Sunday, March 12, 2017 8:39 PM
  • Not sure if I have understood, but maybe :

    if (Double.Parse(textBox3.Text) > 0)
    {
              MessageBox.Show(string.Format("Your answer is {0}", textBox3.Text));
    }

    • Edited by Castorix31 Sunday, March 12, 2017 9:15 PM
    Sunday, March 12, 2017 9:09 PM
  • There is a defect in your code. During subtraction, you are using textBox1.Text on both the places.

    Hence you will always get 0 as an output.

    I guess the second value comes from textbox2. If true then Change your code as below


    textBox3.Text = (Double.Parse(textBox1.Text) - Double.Parse(textBox2.Text)).ToString();
                   
    if(Double.Parse(textBox3.Text) > 0)
    {

    MessageBox.Show(string.Format("Your answer is {0}", textBox3.Text));

    }

       

    Sunday, March 12, 2017 9:59 PM
  • You are on a pretty decent track.

    You should propably do backend calculations as numbers entirely. Adding those Parse opeartions really just makes the code unessasarily hard to read.
    Think in the old IPO Principle, with Parsing being the Input step and writing texts to Textbose and labels being the output step.
    Readability is worth a lot more then shortness of code in the long run.

    double value1, value2;
    value1=Doble.Parse(textbox1.Text);
    value2 = value1 - value1;
    
    if(value2 > 0){
      MessageBox.Show("Your answer is " + value2.ToString());
    }

    Your current code has a bug, in that you substract a value from itself. It never can be anything but 0.
    On the other hand you are also checking if a string is larger then the number 0. Wich make no sense. Just keepin it numbers if you want numbers.
    If you want to use Console Style text formatting, String.Format() is the droid you are looking for. Wich happens to be the same droid Console is calling itself.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.


    Sunday, March 12, 2017 10:15 PM
  • Always use TryParse() when parsing user-entered data ... and I'm assuming that your application user will be entering data into those TextBoxes. Also, personally, I prefer to use decimal instead of double for better accuracy (see my blog post, in case you're interested in the reasons: http://geek-goddess-bonnie.blogspot.com/2013/09/double-vs-decimal.html )

    decimal value1, value2, value3;
    decimal.TryParse(textbox1.Text, out value1);
    decimal.TryParse(textbox2.Text, out value2);
    
    value3 = value1 - value2;
    textBox3.Text = value3.ToString();
    
    if(value3 > 0)
    {
      MessageBox.Show("Your answer is " + textBox3.Text);
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, March 13, 2017 12:54 AM
    Moderator
  • Hi Mweyola777,

    Thank you for posting here.

    For your question, you could try the following code.

        private void button1_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
                {
                    textBox3.Text = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text)).ToString();
                }
                if (Convert.ToInt32(textBox3.Text) > 0)
                {
                    MessageBox.Show("Your answer is " + textBox3.Text);
                }
            }

    I use a gif to show the result.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 13, 2017 2:27 AM
    Moderator
  • textBox3.Text = (Double.Parse(textBox1.Text) - Double.Parse(textBox1.Text)).ToString();
    Check above statement, you are using value of same text box. In this case value of textBox3 always be 0 (zero).

    Thanks & Regards, Gaurav Kumar Arora http://gaurav-arora.com http://mynangal.com

    Monday, March 13, 2017 2:50 AM
    Moderator
  • I think it would be nice if @Mweyola777 came back here and marked the answer that he preferred as being correct for him.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, March 18, 2017 8:13 PM
    Moderator