0

这是下面的代码。

我正在尝试这样做,以便当我单击 nextButton 按钮时,它会循环到我的文本文件中的下 3 个数字。我不知道哦,我在这里应该可以工作:[

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;
        }
    }

}

以上是将文本文件分解为单个数字的类。

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)
        {
            {




                 count++;
                    if (count == (myDataPoints.Count))
                    {
                        count = 0;
                    }



                dataPoint a = myDataPoints.getItem(count);
                textBoxLatitude.Text = a.CurLatitude;
                textBoxLongtitude.Text = a.CurLongtitude;
                textBoxElevation.Text = a.CurElevation;


            }
        }
    }
}

上面是 Windows 窗体命名空间 GPSProject { class dataPoint { private string latitude; 私人字符串经度;私人字符串提升;

        public dataPoint()         //Overloaded incase no value available
        {
            latitude = "No Latitude Specified";
            longtitude = "No Longtitude Specified";
            elevation = "No Elevation Specified";

        }

        public dataPoint(string Latitude, string Longtitude, string Elevation)
        {

            // TODO: Complete member initialization
            this.latitude = Latitude;
            this.longtitude = Longtitude;
            this.elevation = Elevation;

        }

        public string CurLongtitude { get { return this.longtitude; } }

        public string CurLatitude { get { return this.latitude; } }

        public string CurElevation { get { return this.elevation; } }
    }
}                   

最后,这是持有数字的班级。我试图让文本框显示的数字是 CurLongtitude/Latitue/Elevation 的周期

4

1 回答 1

0

首先要做的是为您的数据创建一个合适的容器:DataPoint 实体:

class DataPoint
{
    // Option 1: Field + read only property
    private string _latitude;
    public string Latitude { get { return _latitude; } }

    // Option 2: Property + compiler generated field
    public string Longitude { get; private set; }
    public string Elevation { get; private set; }

    // Constructor
    public DataPoint(string latitude, string longtitude, string elevation)
    {
        // Internally in this class we use fields
        _latitude = latitude;

        // Unless we use property option 2
        this.Longitude = longitude;
        this.Elevation = elevation; 
    }
}     

接下来我们可以向 DataPoint 类添加一个静态方法来从磁盘加载数据点:

public static List<DataPoint> LoadFromFile (string filename) 
{
    // The .NET framework has a lot of helper methods
    // be sure to check them out at MSDN

    // Read the contents of the file into a string array
    string[] lines = File.ReadAllLines(filename);

    // Create the result List
    List<DataPoint> result = new List<DataPoint>();

    // Parse the lines
    for (string line in lines)
    {
        string[] bits = line.Split(',');

        // We're using our own constructor here
        // Do watch out for invalid files, resulting in out-of-index Exceptions
        DataPoint dataPoint = new DataPoint(bits[0], bits[1], bits[2]);
        result.Add(dataPoint);
    }

    return result;
}

现在我们已经有了所有的构建块。让我们制作应用程序:

public partial class Form1 : Form
{
    private int _index;
    private List<DataPoint> _dataPoints;

    public Form1()
    {
        // Since this is a simple test application we'll do the call here
        _dataPoints = DataPoint.LoadFromFile(@"C:\Test.txt");

        InitializeComponent();
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        // Cycle the data points
        _index++;
        if (_index == _dataPoints.Count)
        {
            _index = 0;
        }

        // Get the specific data point
        DataPoint dataPoint = _dataPoints[_index];

        // The empty texts are UI only, so we could check them here
        if (dataPoint.Latitude == null || dataPoint.Latitude == "")
        {
            textBoxLatitude.Text = "No Latitude Specified";
        }
        else
        {
            textBoxLatitude.Text = dataPoint.Latitude;
        }

        // A shorter, inline version
        textBoxLongtitude.Text = String.IsNullOrEmpty(dataPoint.Longitude) ? "No Longitude Specified" : dataPoint.Longitude;

        // Or if we don't care about empty texts
        textBoxElevation.Text = dataPoint.Elevation;
    }
}

当然,有很多方法可以使代码更短,或者使用 LINQ 等现代技术,但我尽量不要离你现有的代码太远。我还没有尝试过代码,我在这里输入了 SO :)

另外请注意如何格式化代码。正确的大小写和遵循标准使您的代码更容易被其他人阅读。

MSDN 有很多关于 .NET Framework 类的优秀示例和大量文档。

于 2012-11-28T01:57:07.360 回答