-1

正如问题所表明的,这就是我正在尝试做的事情,任何人都可以看到一段时间以来一直在摆弄它。目前它只显示文本文件中的前 3 个数字,当我按下下一个按钮时,我希望它转到下一个 3,但它似乎不起作用..

namespace GPSProject
{
    public partial class Form1 : Form
    {
        private int count;
        internal dataPoints myDataPoints;
        public Form1()
        {
            myDataPoints = new dataPoints();
            InitializeComponent();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            {
                Button b = (Button)sender;
                if (b.Name.Equals("buttonNext"))
                {

                    count++;
                    if (count == (myDataPoints.Count))
                        count = 0;
                }
                else
                {
                    count--;
                    if (count < 0)
                        count = myDataPoints.Count - 1;
                }
                dataPoint a = myDataPoints.getItem(count);
                textBoxLatitude.Text = a.CurLatitude;
                textBoxLongtitude.Text = a.CurLongtitude;
                textBoxElevation.Text = a.CurElevation;

            }
        }
    }
}

上面是我的表单窗口,下面是我的数据点

namespace GPSProject
{
    class dataPoints
    {
        public int Count { get { return Points.Count; } }
        List<dataPoint> Points;
        //string p;

        public dataPoints(/*string path*/)
        {
            Points = new List<dataPoint>();
           // p = path;
            TextReader tr = new StreamReader(/*p*/"C:/Test.txt");
            string input;
            while ((input = tr.ReadLine()) != null)
            {
                string[] bits = input.Split(',');
                dataPoint a = new dataPoint(bits[0], bits[1], bits[2]);
                Points.Add(a);                
            }

            tr.Close();
        }          



        internal dataPoint getItem(int p)
        {
            if (p < Points.Count)
            {
                return Points[p];
            }
            else

                return null;
        }
    }

}
4

2 回答 2

1

您需要更新while循环以一次获取 3 个数据项,如下所示:

while ((input = tr.ReadLine()) != null)
{
    string[] bits = input.Split(',');
    for (int i = 0; i < bits.Length / 3; i++)
    {
        dataPoint a = new dataPoint(bits[3*i], bits[3*i+1], bits[3*i+2]);
        Points.Add(a);
    }
}
于 2012-11-27T23:07:39.267 回答
1
string input = "1,2,3,4,5,6,7,8,9,10,11,12";
//string input = File.ReadAllText(/*p*/"C:/Test.txt");

List<List<string>> all = input.Split(',')
                        .Select((s, i) => new { s, i })
                        .GroupBy(x => x.i / 3)
                        .Select(g => g.Select(x=>x.s).ToList())
                        .ToList();

foreach(var bits in all)
{
    Console.WriteLine("{0} {1} {2}", bits[0], bits[1], bits[2]);
    //dataPoint a = new dataPoint(bits[0], bits[1], bits[2]);
    //Points.Add(a); 
}

这将给出一个输出

1 2 3
4 5 6
7 8 9
10 11 12 
于 2012-11-27T23:07:57.587 回答