-1

指数超出范围。必须是非负数且小于集合的大小。参数名称:索引

我有 WireObjectAnimation 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DannyGeneral;

namespace AnimationEditor
{
    class WireObjectAnimation
    {
        private List<WireObjectCoordinates> wocl = new List<WireObjectCoordinates>();

        private WireObject wo1 = null;

        string name;
        int ndx;
        public WireObjectAnimation(string name,WireObject wo)
        {

            this.name = name;

            wo1 = wo;

            WireObjectCoordinates woc;
            woc = new WireObjectCoordinates(wo.woc);
            wocl.Add(woc);
            ndx = 0;

        }





        public void Save(string path , string fileName , PictureBox pb) 
        {
            int framesNumberX = 0;
            int framesNumberY = 0;
            string fn;
            string t = Path.GetFileNameWithoutExtension(this.name);
            if (File.Exists(path + "\\" + "DATABASE" + "\\" + fileName + "\\" + t + ".txt"))
            {
                try
                {
                    string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName);
                    File.Delete(f);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not delete file from disk. Original error: " + ex.Message);
                }


                fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName;
            }
            else
            {
                fn = path + "\\" + "DATABASE" + "\\" + fileName + "\\" + this.name + ".txt";
            }
            OptionsFile setting_file = new OptionsFile(fn);
            setting_file.SetKey("File Name", fn);
            setting_file.SetKey("Object Name", fileName);
            setting_file.SetKey("Animation Name", this.name);
            setting_file.SetKey("picturebox.Width", pb.Width.ToString());
            setting_file.SetKey("picturebox.Height", pb.Height.ToString());

            string[] xFrames = new string[wocl.Count];
            string[] yFrames = new string[wocl.Count];

            string X="";
            string Y="";
            for (int i = 0; i < wocl.Count; i++)
            {
                X  = string.Format("Frame_X_{0} ", i + 1);
                Y  = string.Format("Frame_Y_{0} ", i + 1);
                framesNumberX ++;
                framesNumberY ++;
                for (int j = 0; j < wocl[i].Point_X.Count; j++)
                {
                    xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
                    yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);


                }

                string tt = xFrames[i].Trim(",".ToCharArray());
                string yy =  yFrames[i].Trim(",".ToCharArray());



                 setting_file.SetKey(X, tt);
                 setting_file.SetKey(Y, yy);

            }

            int resultX = framesNumberX / 2;
            int resultY = framesNumberY / 2;
            setting_file.SetKey("Number Of Frames X", resultX.ToString()); 
            setting_file.SetKey("Number Of Frames Y", resultY.ToString());







        }


        public void Load(string path,string fileName)
        {
            int numberofframesX = 0;
            int numberofframesY = 0;
            string framesX = "";
            string framesY = "";
            string X = "";
            string Y = "";
            string t = path + "\\" + fileName;
            OptionsFile setting_file = new OptionsFile(t);
            string XX = setting_file.GetKey("Number Of Frames X");
            string YY = setting_file.GetKey("Number Of Frames Y");
            numberofframesX = Convert.ToInt32(XX);
            numberofframesY = Convert.ToInt32(YY);


            for (int i = 1; i < numberofframesX ; i++)
            {
            X  = string.Format("Frame_X_{0} ", i);
            framesX = setting_file.GetKey(X);
            List<string> floatStrings = new List<string>(framesX.Split(new char[] { ',' }));
            List<float> test = floatStrings.Select(tempStr => (float)Convert.ToDouble(tempStr)).ToList(); 


                wo1.woc.Point_X = test;  

            }
            for (int i = 1; i < numberofframesY; i++)
            {
                Y = string.Format("Frame_Y_{0} ", i);
                framesY = setting_file.GetKey(Y);
                List<string> floatStrings = new List<string>(framesY.Split(new char[] { ',' }));
                List<float> test = floatStrings.Select(tempStr => (float)Convert.ToDouble(tempStr)).ToList();
                wo1.woc.Point_Y = test;
            }
        }


        public void SetFrame(int frameNumber, WireObjectCoordinates woc)
        {
            wocl[frameNumber].Set(woc);
        }

        public WireObjectCoordinates GetFrame(int frameNumber)
        {
            if (frameNumber > wocl.Count)
            {
                throw new Exception("not allowed!");
            }

            if (frameNumber == wocl.Count)
            {
                WireObjectCoordinates woc;
                woc = new WireObjectCoordinates(wocl[wocl.Count - 1]); 

                wocl.Add(woc);
                return woc;
            }
            else
            {

                return wocl[frameNumber];
            }


        }
    }
}

现在,当我加载加载函数时,我看到加载它的点很好。但后来我试图将 trackBar 栏滚动向右移动,然后我得到了异常。现在这条线: wo1.woc.Point_X = test; woc 有 4 个索引,每个索引中的 Point_X 和 Point_Y 都用每个索引中的数字填充。

在这个类中,我有 SetFrame 和 GetFrame 函数,并且我在 trackBar 的 Form1 滚动事件中使用 GetFrame:

private void trackBar1_Scroll(object sender, EventArgs e)
        {

            currentFrameIndex = trackBar1.Value;
            textBox1.Text = "Frame Number : " + trackBar1.Value;
            wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex)); 
            LoadPictureAt(trackBar1.Value, sender);

            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button8.Enabled = false;
            SaveFormPicutreBoxToBitMapIncludingDrawings();

            return;









        }

现在,当我将 trackBar 向右移动一次时,它应该从 Point_X 和 Point_Y 绘制下一组数字,而不是转到 WireObjectCoordinates 类并在那里抛出异常:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
    class WireObjectCoordinates
    {
        public List<float> Point_X = new List<float>();
        public List<float> Point_Y = new List<float>();

        public WireObjectCoordinates()
        {
        }

        public WireObjectCoordinates(WireObjectCoordinates w)
        {
            Point_X.AddRange(w.Point_X);
            Point_Y.AddRange(w.Point_Y);
        }

        public void Set(WireObjectCoordinates w)
        {
            for (int i = 0; i < Point_X.Count; i++)
            {
                Point_X[i] = w.Point_X[i];
                Point_Y[i] = w.Point_Y[i];
            }
        }
    }
}

异常上线:Point_X[i] = w.Point_X[i]; Point_X[i] 现在包含从 [0] 到 [3] 的 4 个索引,每个索引包含一个数字,例如 332.0 333.0 334.0 335.0 而 w.Point_X[i] 现在只包含一个索引 [0],并且该索引的数字为 332.0

我只是不明白为什么这条线上有例外。

这个想法是,当我将 trackBar 向右移动时,它应该从 wo1.woc.Point_Y 和 wo1.woc.Point_X 绘制下一个坐标,但我想我在 Load 函数中做错了什么?我不确定为什么它会抛出异常,并且只有当我将 trackBar 向右移动一次时才会抛出异常。

4

2 回答 2

0

在它周围抛出一个尝试捕获它在技术上不会解决问题,但它不会再崩溃并且只是阅读错误

于 2012-07-19T21:16:12.277 回答
0

你希望你的for循环如何工作?循环变量i从这个实例的列表0计数。Point_X但是如果另一个实例wPoint_X一个较低计数的列表,或者如果Point_Y列表thisPoint_Y列表w有,它将失败。

如果你意识到Point_X.Countis4w.Point_X.Countis only 1,那么当i超过时0,表达式w.Point_X[i]将尝试读取列表中不存在的元素。这会引发“索引超出范围”的异常。

但也许你明白吗?

于 2012-07-19T21:28:54.580 回答