0

I have an application with 20 textboxes, which they do get their values from a database when a selection is made in a combobox. I need to do the calculations (sum the textBoxes values) and display the result on label or in a textbox when the combobox selection is choosen.

I added the texBoxes into an array and then iterate through them. But I’m getting 0 as the result.

The Problem:

enter image description here

My Code:

private void sumTextBoxes()
    {
        TextBox[] txt;
        txt = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10 };

        int value;

        foreach (TextBox tb in txt)
        {
            if(tb !=null)
            {
                if (int.TryParse(tb.Text, out value))
                {
                    if (textBox11.Text.Length > 0)
                    {
                        textBox11.Text = (int.Parse(textBox11.Text) + value).ToString();
                    }
                    else
                        textBox11.Text = value.ToString();
                }
            }


            //label3.Text = Convert.ToString(value);

        }
        }

Where am I wrong? or a better way to solve it? PS: The textboxes on the left 1-10 and the total is 11 and on the right 12-21 total is 22

mikybrain
  • 169
  • 1
  • 4
  • 14

5 Answers5

1

To sum up all the data you can use Linq:

  textBox11.Text = txt
    .Where(item => item != null) // <- Actually you don't need this
    .Select(item => { // form TextBox to its integer value and parse result
      int value;
      Boolean result = int.TryParse(item.Text, out value);
      return new {
        result = result,
        value = value
      };
    })
    .Where(item => item.result) // filter out all that parsed
    .Sum(item => item.value)
    .ToString();

Think on not explicitly putting TextBoxes into array but query them as well:

textBox11.Text = Controls // <- if the TextBoxes are on the form
  .Cast<TextBox>()
  .Where(item => item != textBox11) // <- all TextBoxes except that with a total
  .Select(item => {
...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Here's a key part of the documentation for Int32.Tryparse():

When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

This means you cannot apply sum operations as you might normally (return an int, add to previous result, repeat).

So how, to fix that? A temporary variable and summing the result if the return value of TryParse() is true is one way:

// A mock but the idea is the same            
var tx = new[] { new TextBox { Text = "1" }, new TextBox { Text = "2" }, new TextBox { Text = "" }, new TextBox { Text = "3" } };

var sum = 0;
foreach (TextBox t in tx) {
    var temp = 0;
    if (Int32.TryParse(t.Text, out temp)) {
        sum += temp; 
    }
}

Console.WriteLine(sum);

I imagine you may also be able to come up with a LINQ-ish way of doing this, but that would likely end up being more confusing that anything else. For a task like this I'd stick which readability.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
0

As far as I can see - you're using string field textBox11.Text to store integer data, and converting it to string and back to integer.

If you don't have very strong reason of doing so (probably left out of question scope) - it's better to sum all the integer values into integer variable, not string one.

int value;
int result = 0;

foreach (TextBox tb in txt)
{
    if(tb !=null)
    {
        if (int.TryParse(tb.Text, out value))
        {
            result += value;
        }
    }
}

label3.Text = Convert.ToString(result);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
0

I would be using a DataGridView instead of these many textboxes. Take a look at following working sample (for very simple computing).

    DataTable table = new DataTable();

    private void CalculatedFieldGrid_Load(object sender, EventArgs e)
    {

        table.Columns.Add("Name");
        table.Columns.Add("Count", typeof(int));

        table.Rows.Add();
        table.Rows[0][0] = "One";
        table.Rows[0][1] = 10;

        table.Rows.Add();
        table.Rows[1][0] = "Two";
        table.Rows[1][1] = 20;

        table.Rows.Add();
        table.Rows[2][0] = "Three";
        table.Rows[2][1] = 30;

        table.Rows.Add();
        table.Rows[3][0] = "Four";
        table.Rows[3][1] = 40;

        table.Rows.Add();
        table.Rows[4][0] = "Calculated Value: ";

        dataGridView1.DataSource = table;

        comboBox1.Items.Add("Add");
        comboBox1.Items.Add("Average");
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex == 0)
        {
            table.Rows[4][1] = table.Compute("Sum(Count)", null);
        }
        else if (comboBox1.SelectedIndex == 1)
        {
            table.Rows[4][1] = table.Compute("Avg(Count)", null);
        }
        dataGridView1.DataSource = table;
    }
danish
  • 5,550
  • 2
  • 25
  • 28
-1

The following solution works with doubles, refreshes the sum textbox only once and reduces the parsing work to minimum :

private void sumTextBoxes()
{
    TextBox[] txt;
    txt = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10 };

    double sum = 0;
    double value;

    foreach (TextBox tb in txt)
    {
        if (tb !=null)
        {
            if (double.TryParse(tb.Text, out value))
            {
                sum += value;
            }
        }
    }

    textBox11.Text = sum.ToString();
}
Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • It works with doubles (and it seems useful from printscreen), refreshes textBox11 only once, reduces parsing work. – Bioukh May 05 '15 at 07:30
  • 1
    I mean: Please explain in your answer what you are doing rather than dumping some code. – Patrick Hofman May 05 '15 at 07:30
  • @Bioukh. It works perfectly but an explaination will be cool for the others too. Thnx – mikybrain May 05 '15 at 07:36
  • Can u please explain exactly what this line does? `if (double.TryParse(tb.Text, out value))` – mikybrain May 05 '15 at 07:47
  • `double.TryParse()` does the same than `int.TryParse()` but for doubles instead of integers. In your printscreen, there is one double (819752,52) so it seems to be more appropriate to use `double.TryParse()`. – Bioukh May 05 '15 at 08:55