Question Help for the simple add subtract program

sweetfriend9

New member
Joined
Jul 25, 2017
Messages
2
Programming Experience
Beginner
HI ,
I am making simple windows form application ( screenshot attached ) , both the radio buttons when checked are giving the subtraction result for the below mentioned code :
        private void ClearButton_Click(object sender, EventArgs e)
        {
            FirstNumberTextBox.Clear();
            SecondNumbereTextBox.Clear();
            ResultTextBox.Clear();
            FirstNumberTextBox.Focus();
            AdditionRadioButton.Checked = true;
        }

        private void CalculateButton_Click(object sender, EventArgs e)
        {
            int firstNum = Convert.ToInt32(FirstNumberTextBox.Text);
            int secondNum = Convert.ToInt32(SecondNumbereTextBox.Text);
            int result = 0;
             if (AdditionRadioButton.Checked == true) ;
            {
                result = firstNum + secondNum;
            }
            if (SubstractionRadioButton.Checked == true) ;
            {
                result = firstNum - secondNum;
            }
            ResultTextBox.Text = result.ToString();
        }   
    }
}
 

Attachments

  • Capture.JPG
    Capture.JPG
    27.9 KB · Views: 55
Last edited by a moderator:
HI ,
I am making simple windows form application ( screenshot attached ) , both the radio buttons when checked are giving the subtraction result for the below mentioned code :
        private void ClearButton_Click(object sender, EventArgs e)
        {
            FirstNumberTextBox.Clear();
            SecondNumbereTextBox.Clear();
            ResultTextBox.Clear();
            FirstNumberTextBox.Focus();
            AdditionRadioButton.Checked = true;
        }

        private void CalculateButton_Click(object sender, EventArgs e)
        {
            int firstNum = Convert.ToInt32(FirstNumberTextBox.Text);
            int secondNum = Convert.ToInt32(SecondNumbereTextBox.Text);
            int result = 0;
             if (AdditionRadioButton.Checked == true) ;
            {
                result = firstNum + secondNum;
            }
            if (SubstractionRadioButton.Checked == true) ;
            {
                result = firstNum - secondNum;
            }
            ResultTextBox.Text = result.ToString();
        }   
    }
}
From the code you've provided it should work as you intend, unless only of the two RadioButton's is in the Operation's GroupBox, that would allow both to be selected at the same time and would mean the subtraction would always be the final result.

I was able to reproduce this app without the RadioButton issue:
public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void CalculateButton_Click(object sender, EventArgs e) {
        int Number1 = 0;
        int Number2 = 0;
        ResultTextBox.Clear();

        if (int.TryParse(FirstNumberTextBox.Text.Trim(), out Number1) && int.TryParse(SecondNumberTextBox.Text.Trim(), out Number2)) {
            double Result = 0.0;

            if (AdditionRadioButton.Checked)
                Result = Number1 + Number2;
            else if (SubtractionRadioButton.Checked)
                Result = Number1 - Number2;
            else if (MultiplicationRadioButton.Checked)
                Result = Number1 * Number2;
            else
                Result = Number1 / Number2;

            ResultTextBox.Text = Result.ToString();
        } else {
            MessageBox.Show("Please enter two integer numbers", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void FirstNumberTextBox_TextChanged(object sender, EventArgs e) {
        ResultTextBox.Clear();
    }

    private void AdditionRadioButton_CheckedChanged(object sender, EventArgs e) {
        ResultTextBox.Clear();
    }
}
 

Attachments

  • AddSubtract.zip
    16.7 KB · Views: 42
Back
Top Bottom