0

这是我的情况:我可以从数据库中检索数据,但是当我运行程序并填写Quantity列(在第三行)时,错误显示:索引超出范围。有谁知道为什么会这样?(当我尝试填写数量列(在第三行)时,出现错误)<-如下图所示。

这是我的程序的图像: 当我尝试填写 *Quantity* 列(在第三行)时,出现了错误

这是错误所在位置的图像: 在此处输入图像描述

这是完整的代码:

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

namespace Sell_System
{
    public partial class Form2 : Form
    {
        string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");
        private Form1 firstForm;
        private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxAllTotalContainer = new List<List<TextBox>>();

        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 firstForm)
            : this()
        {
            this.firstForm = firstForm;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            UpdateTextPosition();

            OleDbDataReader dReader;
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);

            dReader = cmd.ExecuteReader();

            AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();

            while (dReader.Read())
            {
                string numString = dReader[0].ToString().PadLeft(4, '0');
                codesCollection.Add(numString);
            }

            dReader.Close();
            conn.Close();

            if (firstForm.comboBox1.SelectedIndex == 0)
            {
                label1.Text = "Code:";
                label1.Location = new Point(60, 125);
                label2.Text = "Welcome to the Selling System.";
                label2.Location = new Point(600, 0);
                label3.Text = "Quantity:";
                label3.Location = new Point(155, 125);
                label4.Text = "Description:";
                label4.Location = new Point(580, 125);
                label5.Text = "Sub Total on Rp:";
                label5.Location = new Point(1020, 125);
                label6.Text = "Total on Rp:";
                label6.Location = new Point(1210, 125);
                label7.Text = "Total on Rp:";
                label7.Location = new Point(1080, 580);
            }

            else if (firstForm.comboBox1.SelectedIndex == 1)
            {
                label1.Text = "Kode:";
                label1.Location = new Point(60, 125);
                label2.Text = "Selamat datang di Selling System.";
                label2.Location = new Point(600, 0);
                label3.Text = "Banyaknya:";
                label3.Location = new Point(145, 125);
                label4.Text = "Keterangan:";
                label4.Location = new Point(580, 125);
                label5.Text = "Sub Total di Rp:";
                label5.Location = new Point(1020, 125);
                label6.Text = "Total di Rp:";
                label6.Location = new Point(1210, 125);
                label7.Text = "Total di Rp:";
                label7.Location = new Point(1080, 580);
            }

            //****TextBox for Code****
            for (int y = 0; y <= 16; y++)
            {
                textBoxCodeContainer.Add(new List<TextBox>());
                textBoxCodeContainer[0].Add(new TextBox());
                textBoxCodeContainer[0][y].Size = new Size(100, 50);
                textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));
                textBoxCodeContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

                textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
                textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;

                Controls.Add(textBoxCodeContainer[0][y]);
            }

            //****TextBox for Quantity****
            for (int y = 0; y <= 16; y++)
            {
                textBoxQuantityContainer.Add(new List<TextBox>());
                textBoxQuantityContainer[0].Add(new TextBox());
                textBoxQuantityContainer[0][y].Size = new Size(100, 50);
                textBoxQuantityContainer[0][y].Location = new Point(125, 150 + (y * 25));
                textBoxQuantityContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

                Controls.Add(textBoxQuantityContainer[0][y]);
            }

            //****TextBox for Description****
            for (int y = 0; y <= 16; y++)
            {
                textBoxDescContainer.Add(new List<TextBox>());
                textBoxDescContainer[0].Add(new TextBox());
                textBoxDescContainer[0][y].Size = new Size(750, 50);
                textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25));

                Controls.Add(textBoxDescContainer[0][y]);
            }

            //****TextBox for Sub Total****
            for (int y = 0; y <= 16; y++)
            {
                textBoxSubTotalContainer.Add(new List<TextBox>());
                textBoxSubTotalContainer[0].Add(new TextBox());
                textBoxSubTotalContainer[0][y].Size = new Size(175, 50);
                textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25));

                Controls.Add(textBoxSubTotalContainer[0][y]);
            }

            //****TextBox for Total****
            for (int y = 0; y <= 16; y++)
            {
                textBoxTotalContainer.Add(new List<TextBox>());
                textBoxTotalContainer[0].Add(new TextBox());
                textBoxTotalContainer[0][y].Size = new Size(175, 50);
                textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25));
                textBoxTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

                Controls.Add(textBoxTotalContainer[0][y]);
            }

//****TextBox for Total All****
            textBoxAllTotalContainer.Size = new Size(175, 50);
            textBoxAllTotalContainer.Location = new Point(1150, 575);
            textBoxAllTotalContainer.TextChanged += new System.EventHandler(this.textBox_TextChanged);

            Controls.Add(textBoxAllTotalContainer);

        }

        private void UpdateTextPosition()
        {
            Graphics g = this.CreateGraphics();
            Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
            Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
            String tmp = " ";
            Double tmpWidth = 0;

            while ((tmpWidth + widthOfASpace) < startingPoint)
            {
                tmp += " ";
                tmpWidth += widthOfASpace;
            }

            this.Text = tmp + this.Text.Trim();
        }

        private void UpdateDatas()
        {
            int codeValue = 0;
            int index = 0;

            string query = "SELECT [Description], [Price] FROM [Data] WHERE [Code] IN (";

            OleDbDataReader dReader;
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();

            if (int.TryParse(this.textBoxCodeContainer[0][0].Text, out codeValue))
            {
                query = query + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][1].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][2].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][3].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][4].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][5].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][6].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][7].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][8].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][9].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][10].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][11].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][12].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][13].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][14].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][15].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            if (int.TryParse(this.textBoxCodeContainer[0][16].Text, out codeValue))
            {
                query = query + "," + codeValue.ToString();
            }

            query = query + ")";

            OleDbCommand cmd = new OleDbCommand(query, conn);

            cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);

            dReader = cmd.ExecuteReader();

            while (dReader.Read())
            {
                if (textBoxCodeContainer[0][index].TextLength != 0)
                {
                    this.textBoxDescContainer[0][index].Text = dReader["Description"].ToString();
                    this.textBoxSubTotalContainer[0][index].Text = dReader["Price"].ToString();
                }

                index += 1;
            }

            dReader.Close();
            conn.Close();
        }

        private void UpdatePrice()
        {
            if (textBoxQuantityContainer[0][0].Text == "")
            {
                textBoxTotalContainer[0][0].Text = "";
            }

            else if (textBoxQuantityContainer[0][0].Text == "1")
            {
                textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
            }

            if (textBoxQuantityContainer[0][1].Text == "")
            {
                textBoxTotalContainer[0][1].Text = "";
            }

            else if (textBoxQuantityContainer[0][1].Text == "1")
            {
                textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
            }

            if (textBoxQuantityContainer[0][2].Text == "")
            {
                textBoxTotalContainer[0][2].Text = "";
            }

            else if (textBoxQuantityContainer[0][2].Text == "1")
            {
                textBoxTotalContainer[0][2].Text = textBoxSubTotalContainer[0][2].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][2].Text;
            }

            if (textBoxQuantityContainer[0][3].Text == "")
            {
                textBoxTotalContainer[0][3].Text = "";
            }

            else if (textBoxQuantityContainer[0][3].Text == "1")
            {
                textBoxTotalContainer[0][3].Text = textBoxSubTotalContainer[0][3].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][3].Text;
            }

            if (textBoxQuantityContainer[0][4].Text == "")
            {
                textBoxTotalContainer[0][4].Text = "";
            }

            else if (textBoxQuantityContainer[0][4].Text == "1")
            {
                textBoxTotalContainer[0][4].Text = textBoxSubTotalContainer[0][4].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][4].Text;
            }

            if (textBoxQuantityContainer[0][5].Text == "")
            {
                textBoxTotalContainer[0][5].Text = "";
            }

            else if (textBoxQuantityContainer[0][5].Text == "1")
            {
                textBoxTotalContainer[0][5].Text = textBoxSubTotalContainer[0][5].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][5].Text;
            }

            if (textBoxQuantityContainer[0][6].Text == "")
            {
                textBoxTotalContainer[0][6].Text = "";
            }

            else if (textBoxQuantityContainer[0][6].Text == "1")
            {
                textBoxTotalContainer[0][6].Text = textBoxSubTotalContainer[0][6].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][6].Text;
            }

            if (textBoxQuantityContainer[0][7].Text == "")
            {
                textBoxTotalContainer[0][7].Text = "";
            }

            else if (textBoxQuantityContainer[0][7].Text == "1")
            {
                textBoxTotalContainer[0][7].Text = textBoxSubTotalContainer[0][7].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][7].Text;
            }

            if (textBoxQuantityContainer[0][8].Text == "")
            {
                textBoxTotalContainer[0][8].Text = "";
            }

            else if (textBoxQuantityContainer[0][8].Text == "1")
            {
                textBoxTotalContainer[0][8].Text = textBoxSubTotalContainer[0][8].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][8].Text;
            }

            if (textBoxQuantityContainer[0][9].Text == "")
            {
                textBoxTotalContainer[0][9].Text = "";
            }

            else if (textBoxQuantityContainer[0][9].Text == "1")
            {
                textBoxTotalContainer[0][9].Text = textBoxSubTotalContainer[0][9].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][9].Text;
            }

            if (textBoxQuantityContainer[0][10].Text == "")
            {
                textBoxTotalContainer[0][10].Text = "";
            }

            else if (textBoxQuantityContainer[0][10].Text == "1")
            {
                textBoxTotalContainer[0][10].Text = textBoxSubTotalContainer[0][10].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][10].Text;
            }

            if (textBoxQuantityContainer[0][11].Text == "")
            {
                textBoxTotalContainer[0][11].Text = "";
            }

            else if (textBoxQuantityContainer[0][11].Text == "1")
            {
                textBoxTotalContainer[0][11].Text = textBoxSubTotalContainer[0][11].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][11].Text;
            }

            if (textBoxQuantityContainer[0][12].Text == "")
            {
                textBoxTotalContainer[0][12].Text = "";
            }

            else if (textBoxQuantityContainer[0][12].Text == "1")
            {
                textBoxTotalContainer[0][12].Text = textBoxSubTotalContainer[0][12].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][12].Text;
            }

            if (textBoxQuantityContainer[0][13].Text == "")
            {
                textBoxTotalContainer[0][13].Text = "";
            }

            else if (textBoxQuantityContainer[0][13].Text == "1")
            {
                textBoxTotalContainer[0][13].Text = textBoxSubTotalContainer[0][13].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][13].Text;
            }

            if (textBoxQuantityContainer[0][14].Text == "")
            {
                textBoxTotalContainer[0][14].Text = "";
            }

            else if (textBoxQuantityContainer[0][14].Text == "1")
            {
                textBoxTotalContainer[0][14].Text = textBoxSubTotalContainer[0][14].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][14].Text;
            }

            if (textBoxQuantityContainer[0][15].Text == "")
            {
                textBoxTotalContainer[0][15].Text = "";
            }

            else if (textBoxQuantityContainer[0][15].Text == "1")
            {
                textBoxTotalContainer[0][15].Text = textBoxSubTotalContainer[0][15].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][15].Text;
            }

            if (textBoxQuantityContainer[0][16].Text == "")
            {
                textBoxTotalContainer[0][16].Text = "";
            }

            else if (textBoxQuantityContainer[0][16].Text == "1")
            {
                textBoxTotalContainer[0][16].Text = textBoxSubTotalContainer[0][16].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][16].Text;
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            UpdateDatas();
            UpdatePrice();
        }

    }
}

这是我的问题:“我很困惑,当我尝试为“textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text”分配一个值时,代码正在运行。但是,当我尝试“textBoxAllTotalContainer.Text = textBoxTotalContainer[ 0][one(1)]",屏幕卡在那里(我必须停止从 Visual Studio 调试)"

textBoxTotalContainer 包含 17 个文本框,因此,我使用 [0][0] 到 [0][16],而 textBoxAllTotalContainer 包含 1 个文本框,所以我只使用 textBoxAllTotalContainer.Text

注意:“关键字(一),它假设为“1”<--数字,我写(一),因为当我尝试更改[0] [(这里应该是“1”)]时,文本自动更改为链接图像”

问题:**“当我尝试代码时

else if (textBoxQuantityContainer[0][0].Text == "1")
            {
                textBoxTotalContainer[0][0].Text = textBoxSubTotalContainer[0][0].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][0].Text;
            }

alltotalcontainer的文本框已打开(Rp 上的总计,文本框位于单词之后)在上图中,小计的文本框已打开(Rp 上的小计),totalcontainer的文本框已打开(Rp 上的总计,文本框位于文字下方),数量容器的文本框位于(数量)。在上面的代码中,显示textboxtotalcontainer上的文本框 1 与subtotalcontainer上的文本框 1 相同,并且alltotalcontainertotalcontainer相同。**

但是,下面的代码它不起作用,屏幕卡在那里(鼠标消失,我什么也做不了,除非我在 Visual Studio 上按 SHIFT + F5):

else if (textBoxQuantityContainer[0][1].Text == "1")
            {
                textBoxTotalContainer[0][1].Text = textBoxSubTotalContainer[0][1].Text;
                textBoxAllTotalContainer.Text = textBoxTotalContainer[0][1].Text;
            }

谢谢

4

2 回答 2

1

当您尝试访问不存在的集合中的元素时,您会收到该错误。

例如,如果您有一个字符串列表...

List<string> myList = new List {"one", "two", "three"};

...并尝试使用 访问第四个元素myList[3],您将收到该错误。

要么textBoxAllTotalContainer[0][2]不存在,要么textBoxTotalContainer[0][2]。无法从您图片中的错误中看出。在该行上放置一个断点并检查两个对象。

于 2013-08-18T00:54:53.410 回答
0

看起来你的停止条件是错误的。
您有以下用于初始化的代码textBoxAllTotalContainer

        //****TextBox for Total All****
        for (int y = 0; y <= 1; y++)
        {
            textBoxAllTotalContainer.Add(new List<TextBox>());
            textBoxAllTotalContainer[0].Add(new TextBox());
            textBoxAllTotalContainer[0][y].Size = new Size(175, 50);
            textBoxAllTotalContainer[0][y].Location = new Point(1150, 575);
            textBoxAllTotalContainer[0][y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

            Controls.Add(textBoxAllTotalContainer[0][y]);
        }

请注意,循环仅迭代两次。因此textBoxAllTotalContainer[0][0]textBoxAllTotalContainer[0][1]被创建。

但是当您尝试访问时,textBoxAllTotalContainer[0][2]您会遇到异常,因为该位置确实超出了异常指定的列表范围。

于 2013-08-18T06:04:41.873 回答