Sum of Odd and Even number in multiple textbox.

YUI-012 160 Reputation points
2024-10-05T00:21:10.2733333+00:00

Good day, I want to ask some help.

User's image

As you can see the image above it display odd numbers and even numbers, now I want to get the sum of odd numbers and even numbers.

private void TextBox11_TextChanged(object sender, EventArgs e)
        {
            if (textBox11.Text == "")
            {
                textBox11.BackColor = Color.Red;
                DialogResult result = MessageBox.Show("This fild is empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (result == DialogResult.OK)
                {
                    textBox11.BackColor = Color.White;
                    this.Refresh();
                }
            }
            else 
            {
                int a = 0;
                a = Convert.ToInt32(textBox11.Text);
                if (a % 2 == 0)
                {
                    label5.ForeColor = Color.Violet;
                    label5.Text = "EVEN";
                }
                else
                {
                    label5.ForeColor = Color.Blue;
                    label5.Text = "ODD";
                }
            }
        }


The code above I use every textbox to display odd or even in the label.

My problem now is how to get the sum of odd numbers and even number.

Developer technologies Visual Studio Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,655 Reputation points
    2024-10-06T18:29:42.7166667+00:00

    Arrays make programming easier.

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
        private readonly TextBox[] textBoxes;
        private readonly Label[] labels;
    
        public Form1() {
            InitializeComponent();
            textBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
            labels = new Label[] { label1, label2, label3, label4};
            foreach (TextBox textBox in textBoxes) {
                textBox.TextChanged += TextBox_TextChanged;
            }
        }
    
        private void TextBox_TextChanged(object sender, EventArgs e) {
            var textBox = (TextBox)sender;
            var label = labels[Array.IndexOf(textBoxes, textBox)];
            if (textBox.Text == "") {
                textBox.BackColor = Color.Red;
                DialogResult result = MessageBox.Show("This fild is empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (result == DialogResult.OK) {
                    textBox.BackColor = Color.White;
                    this.Refresh();
                }
            } else {
                int a = Convert.ToInt32(textBox.Text);
                if (a % 2 == 0) {
                    label.ForeColor = Color.Violet;
                    label.Text = "EVEN";
                } else {
                    label.ForeColor = Color.Blue;
                    label.Text = "ODD";
                }
            }
    
            int even = 0, odd = 0;
            foreach (var box in textBoxes) {
                int.TryParse(box.Text, out int value);
                if (value % 2 == 0) {
                    even += value;
                } else {
                    odd += value;
                }
            }
            lblEven.Text = even.ToString();
            lblOdd.Text = odd.ToString();
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.