简体   繁体   English

为什么单击退出时我的C#代码在消息框中出现零?

[英]Why is my C# code coming up with zero's in message box upon clicking exit?

I am trying to get my message box to show the invoice subtotals stored in my array...5 of them to show in a message box using the foreach method. 我试图让我的消息框显示存储在我的数组中的发票小计...使用foreach方法在消息框中显示其中的5个。 I am supposed to input a wage and it then does some calculation and stores the subtotal value into the array. 我应该输入工资,然后进行一些计算并将小计的值存储到数组中。 I declared an array and index called decArray and intIndex. 我声明了一个数组和索引decArray和intIndex。 Can anyone tell me what I'm missing or doing wrong? 谁能告诉我我缺少或做错了什么? Thank you in advance! 先感谢您!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InvoiceTotal
{

    public partial class frmInvoiceTotal : Form
    {
        public frmInvoiceTotal()
        {
            InitializeComponent();
        }

        // TODO: declare class variables for array and list here
        decimal[] decArray = new decimal[5];
        int intIndex = 0;

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSubtotal.Text == "")
                {
                    MessageBox.Show(
                        "Subtotal is a required field.", "Entry Error");
                }
                else
                {
                    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                    if (subtotal > 0 && subtotal < 10000)
                    {
                        decimal discountPercent = 0m;
                        if (subtotal >= 500)
                            discountPercent = .2m;
                        else if (subtotal >= 250 & subtotal < 500)
                            discountPercent = .15m;
                        else if (subtotal >= 100 & subtotal < 250)
                            discountPercent = .1m;
                        decimal discountAmount = subtotal * discountPercent;
                        decimal invoiceTotal = subtotal - discountAmount;

                        discountAmount = Math.Round(discountAmount, 2);
                        invoiceTotal = Math.Round(invoiceTotal, 2);

                        txtDiscountPercent.Text = discountPercent.ToString("p1");
                        txtDiscountAmount.Text = discountAmount.ToString();
                        txtTotal.Text = invoiceTotal.ToString();

                        for (intIndex = 0; intIndex <= decArray.Length - 1; intIndex++)
                        {
                             DecArray[intIndex] = InvoiceTotal
                        }

                    }
                    else
                    {
                        MessageBox.Show(
                            "Subtotal must be greater than 0 and less than 10,000.", 
                            "Entry Error");
                    }
                }
            }
            catch (FormatException)
            {
                MessageBox.Show(
                    "Please enter a valid number for the Subtotal field.", 
                    "Entry Error");
            }
            txtSubtotal.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // TODO: add code that displays dialog boxes here
            string totalstring = "";
            foreach (decimal value in decArray)
            {
                totalstring += value + "\n";
                MessageBox.Show(totalstring + "\n", "Order Totals");
            }
            this.Close();
        }
    }
}

您永远不会分配给decArray (例如decArray[0] = n;

If you add a count variable to increment your arrays count then you could add more than the one amount. 如果添加一个count变量来增加数组的数量,则可以添加多个。 You would also want to allow the array to resize as needed. 您还希望允许根据需要调整数组的大小。

decimal[] decArray = new decimal[5];
int _indexCount = 0;

private void btnCalculate_Click(object sender, EventArgs e)
{
    ...
    if (decArray.Count() == _indexCount)
    {
        var elementHolder = decArray;
        decArray = new T[(decArray.Length + 1) * 2];

        for (int i = 0; i < elementHolder.Length; i++)
        {
            decArray[i] = elementHolder[i];
        }
    }

    decArray[_indexCount] = invoiceTotal;
    _indexCount++;

}

Something like that should work. 这样的事情应该起作用。

Edit: Reason you get so many Messages is because the MessageBox.Show() is inside the foreach loop just put it outside the loop and you will only see one. 编辑:之所以收到这么多消息,是因为MessageBox.Show()位于foreach循环内,只是将其放在循环外,您只会看到一个。

    private void btnExit_Click(object sender, EventArgs e)
    {
        // TODO: add code that displays dialog boxes here
        string totalstring = "";
        foreach (decimal value in decArray)
        {
            totalstring += value + "\n";
        }
        MessageBox.Show(totalstring + "\n", "Order Totals");
        this.Close();
    }

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

相关问题 为什么在我的C#代码中没有通过此(布尔)字段? - Why is this bit (Boolean) field not coming through in my C# code? C#退出消息框弹出两次。 我想生成一次事件。 - C# The exit message box pops up twice. I want to generate an event once. 尝试在C#中使用ESC将代码编程到Exit应用程序? - Trying to program code to Exit application upon ESC in C#? 根据组合框 c# 的选择,我的代码没有切换到所需的 function - My code is not switching to the desired function depending upon the selection from combo box c# 为什么打印消息不会在我的C#代码中打印? - Why won't the printing message print in my C# code? 谁能帮我在C#中的代码在消息框中始终输出0 - can anyone help me with my code in c# it always outputs 0 in message box 始终检查特定文件是否存在但消息框始终显示的C#代码行,为什么会这样? - Line of C# code that always checking if a specific file exists but the message box is always showing, why is that? C#中的AES加密功能有什么问题? 为什么返回零长度数据? - What's wrong with my AES encryption function in C#? Why does it return zero length data? 为什么我的C#类告诉我没有带零参数的构造函数? - Why is my C# class telling me there's no constructor with zero arguments? C#中的消息框 - Message Box in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM