ArgumentOutOfRangeException未处理?

时间:2013-08-18 00:46:06

标签: c# database winforms

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

以下是我的计划的图片: When i tried to fill the *Quantity* column (on third line), the error appeared

以下是错误指向的图像: enter image description here

以下是完整代码:

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“&lt; - 数字,我写(一),因为当我尝试改变[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上的总计,文本框位于单词下面), quantitycontainer 的文本框打开(Quantity)。在上面的代码中,显示 textboxtotalcontainer 上的文本框1与 subtotalcontainer 上的文本框1相同,而 alltotalcontainer totalcontainer <相同/ em>的。**

但是,在代码下面它不起作用,屏幕卡在那里(鼠标消失了,我什么都做不了,除非我在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;
            }

由于

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分: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]时,您会遇到异常,因为该位置实际上超出了列表范围,因为异常指定了。