简体   繁体   English

如何使用TextBox对Listbox中的所有数字求和?

[英]How to sum all numbers in a Listbox using TextBox?

I want to count and sum all values in a ListBox . 我想对ListBox所有值进行计数和求和。 For example, I have the following values in my ListBox : 4 , 6 , 1 , 7 . 例如,我在我的下列值ListBox4617 My current value is 18 . 我现在的价值是18 If I add a 2 with a TextBox , I need to get 20 and if I add 5 I need to get 25 in total. 如果我用TextBox添加2 ,我需要20 ,如果我添加5我需要总共25

If I try it with the below code, it gives me a whole other number. 如果我使用下面的代码尝试它,它给了我一个完整的其他数字。

Here is my code: 这是我的代码:

private void AddButton_Click(object sender, EventArgs e)
{
    decimal sum;
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = "";

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        sum += Convert.ToDecimal(listBox2.Items[i].ToString());
    }

    Label1.Text = sum.ToString();
}

You missed to initialize default value of sum. 您错过了初始化sum的默认值。 Assign sum = 0 before adding values to sum variable 在将值添加到sum变量之前分配sum = 0

private void AddButton_Click(object sender, EventArgs e)
{
    decimal sum = 0;  //Set sum = 0  by default
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = "";

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        //To check value of sum after each iteration, you can print it on console
        Console.WriteLine("Sum =" +sum);
        sum += Convert.ToDecimal(listBox2.Items[i].ToString());
    }

    Label1.Text = sum.ToString();
}

Sorry for maybe not answer but it is strange that your compiler didn't tell you to initialize sum. 很抱歉也许没有回答,但奇怪的是你的编译器没有告诉你初始化总和。 The second I tested your code and it works correctly as expected that means that if the problem is not in sum variable then you made something else to this fields somewhere else so your code doesn't work correctly. 第二个我测试了你的代码,它按预期正常工作,这意味着如果问题不在sum变量中,那么你在其他地方为这个字段做了别的东西,所以你的代码不能正常工作。

Having in mind your comment to previous person I'd say the same. 考虑到你对前一个人的评论,我会说同样的。 In some cases (I know it's funny but) you may have viruses on your computer. 在某些情况下(我知道这很有趣但是)你的计算机上可能有病毒。 Check it. 核实。 Once in my life I've failed my maths lab work because of virus that interrupted my program so it drew the wrong chart !sick I know :D 一旦进入我的生活,我的数学实验室工作失败了,因为病毒中断了我的程序所以它绘制了错误的图表!生病我知道:D

    private void button1_Click(object sender, EventArgs e)
    {
        var sum = 0;
        var value = 0;

        listBox1.Items.Add(textBox1.Text);

        foreach (var item in listBox1.Items)
        {
            if (!int.TryParse(item.ToString(), out value))
                continue;

            sum = sum + value;
        }

        label1.Text = sum.ToString();
        textBox1.Text = string.Empty;
    }

Use of unassigned local variable 'sum', sum must be assigned before use ! 使用未分配的局部变量'sum',sum必须在使用前分配!

decimal sum = 0;

Rest all is ok 休息一切都好

Need to set sum to 0 and also it is probably best to use a foreach instead of a Dotloop. 需要将sum设置为0,并且最好使用foreach而不是Dotloop。

private void AddButton_Click(object sender, EventArgs e) {
    decimal sum = 0;
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = ""; 
    foreach (string s in listBox2) { 
        sum += Convert.ToDecimal(s);
    } 
    Label1.Text = sum.ToString(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM