使用C#中的set方法添加数据时出现问题

时间:2012-12-15 04:06:29

标签: c#

您好我正在尝试处理一个项目,其中应用程序打开.csv文件并从中读取数据并显示静态地图中的不同点。我还想为用户添加一个选项,在数据列表中添加纬度,经度等。应用程序中的列表视图仅在打开.csv文件时出现。但我很难添加新的gps指向.csv文件。我的代码在这里给出

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CourseworkExample
{
    public partial class Form1 : Form
    {
        public GPSDataPoint gpsdp;
        List<GPSDataPoint> data;
        List<PictureBox> pictures;
        List<TabPage> tabs;
        public static int pn = 0;
        private TabPage currentComponent;
        private Bitmap bmp1;
        string[] symbols = {  "hospital", "university" };
        Image[] symbolImages;
        ListBox lb = new ListBox();

        public Form1()
        {
            InitializeComponent();
            data = new List<GPSDataPoint>();
            pictures = new List<PictureBox>();
            tabs = new List<TabPage>();
            symbolImages = new Image[symbols.Length];
            for (int i = 0; i < 2; i++)
            {
                string location = "data/" + symbols[i] + ".png";
                symbolImages[i] = Image.FromFile(location);

            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileDialog ofd = new OpenFileDialog();
            string filter = "CSV File (*.csv)|*.csv";
            ofd.Filter = filter;
            DialogResult dr = ofd.ShowDialog();
            if (dr.Equals(DialogResult.OK))
            {
                int i = ofd.FileName.LastIndexOf("\\");

                string name = ofd.FileName;
                string path = ofd.FileName;
                if (i > 0)
                {
                    name = ofd.FileName.Substring(i + 1);
                    path = ofd.FileName.Substring(0, i + 1);
                }
                TextReader input = new StreamReader(ofd.FileName);
                string mapName = input.ReadLine();
                GPSDataPoint gpsD = new GPSDataPoint();
                gpsD.setBounds(input.ReadLine());
                string s;
                while ((s = input.ReadLine()) != null)
                {
                    gpsD.addWaypoint(s);
                }
                input.Close();
                TabPage tabPage = new TabPage();
                tabPage.Location = new System.Drawing.Point(4, 22);
                tabPage.Name = "tabPage" + pn;
                ListBox lb = new ListBox();
                lb.Width = 300;
                int selectedindex = lb.SelectedIndex;


                lb.Items.Add(mapName);
                lb.Items.Add("Bounds");
                lb.Items.Add(gpsD.Bounds[0] + " " + gpsD.Bounds[1] + " " + gpsD.Bounds[2] + " " + gpsD.Bounds[3]);
                lb.Items.Add("Waypoint");
                foreach (WayPoint wp in gpsD.DataList)
                {
                    lb.Items.Add(wp.Name + " " + wp.Latitude + " " + wp.Longitude + " " + wp.Ele + " " + wp.Sym);
                }
                tabPage.Controls.Add(lb);
                pn++;
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Size = new System.Drawing.Size(192, 74);
                tabPage.TabIndex = 0;
                tabPage.Text = name;
                tabPage.UseVisualStyleBackColor = true;
                tabs.Add(tabPage);
                tabControl1.Controls.Add(tabPage);
                tabPage = new TabPage();
                tabPage.Location = new System.Drawing.Point(4, 22);
                tabPage.Name = "tabPage" + pn;
                pn++;
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Size = new System.Drawing.Size(192, 74);
                tabPage.TabIndex = 0;
                tabPage.Text = mapName;
                string location = path + mapName;
                tabPage.UseVisualStyleBackColor = true;
                tabs.Add(tabPage);
                PictureBox pb = new PictureBox();
                pb.Name = "pictureBox" + pn;
                pb.Image = Image.FromFile(location);
                tabControl2.Controls.Add(tabPage);
                pb.Width = pb.Image.Width;
                pb.Height = pb.Image.Height;
                tabPage.Controls.Add(pb);
                currentComponent = tabPage;
                tabPage.Width = pb.Width;
                tabPage.Height = pb.Height;
                pn++;
                tabControl2.Width = pb.Width;
                tabControl2.Height = pb.Height;
                bmp1 = (Bitmap)pb.Image;
                int lx, ly;
                float realWidth = gpsD.Bounds[1] - gpsD.Bounds[3];
                float imageW = pb.Image.Width;
                float dx = imageW * (gpsD.Bounds[1] - gpsD.getWayPoint(0).Longitude) / realWidth;
                float realHeight = gpsD.Bounds[0] - gpsD.Bounds[2];
                float imageH = pb.Image.Height;
                float dy = imageH * (gpsD.Bounds[0] - gpsD.getWayPoint(0).Latitude) / realHeight;
                lx = (int)dx;
                ly = (int)dy;

                using (Graphics g = Graphics.FromImage(bmp1))
                {
                    Rectangle rect = new Rectangle(lx, ly, 20, 20);
                    if (gpsD.getWayPoint(0).Sym.Equals(""))
                    {
                        g.DrawRectangle(new Pen(Color.Red), rect);
                    }
                    else
                    {
                        if (gpsD.getWayPoint(0).Sym.Equals("hospital"))
                        {
                            g.DrawImage(symbolImages[0], rect);
                        }
                        else
                        {
                            if (gpsD.getWayPoint(0).Sym.Equals("university"))
                            {
                                g.DrawImage(symbolImages[1], rect);
                            }
                        }
                    }
                }
                pb.Image = bmp1;
                pb.Invalidate();

            }
        }

        private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            FileDialog ofd = new OpenFileDialog();
            string filter = "CSV File (*.csv)|*.csv";
            ofd.Filter = filter;
            DialogResult dr = ofd.ShowDialog();
            if (dr.Equals(DialogResult.OK))
            {
                int i = ofd.FileName.LastIndexOf("\\");

                string name = ofd.FileName;
                string path = ofd.FileName;
                if (i > 0)
                {
                    name = ofd.FileName.Substring(i + 1);
                    path = ofd.FileName.Substring(0, i + 1);
                }
                TextReader input = new StreamReader(ofd.FileName);
                string mapName = input.ReadLine();
                GPSDataPoint gpsD = new GPSDataPoint();
                gpsD.setBounds(input.ReadLine());
                string s;
                while ((s = input.ReadLine()) != null)
                {
                    gpsD.addWaypoint(s);
                }
                input.Close();
                TabPage tabPage = new TabPage();
                tabPage.Location = new System.Drawing.Point(4, 22);
                tabPage.Name = "tabPage" + pn;
                ListBox lb = new ListBox();
                lb.Width = 300;
                lb.Items.Add(mapName);
                lb.Items.Add("Bounds");
                lb.Items.Add(gpsD.Bounds[0] + " " + gpsD.Bounds[1] + " " + gpsD.Bounds[2] + " " + gpsD.Bounds[3]);
                lb.Items.Add("Waypoint");
                foreach (WayPoint wp in gpsD.DataList)
                {
                    lb.Items.Add(wp.Name + " " + wp.Latitude + " " + wp.Longitude + " " + wp.Ele + " " + wp.Sym);
                }
                tabPage.Controls.Add(lb);
                pn++;
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Size = new System.Drawing.Size(192, 74);
                tabPage.TabIndex = 0;
                tabPage.Text = name;
                tabPage.UseVisualStyleBackColor = true;
                tabs.Add(tabPage);
                tabControl1.Controls.Add(tabPage);
                tabPage = new TabPage();
                tabPage.Location = new System.Drawing.Point(4, 22);
                tabPage.Name = "tabPage" + pn;
                pn++;
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Size = new System.Drawing.Size(192, 74);
                tabPage.TabIndex = 0;
                tabPage.Text = mapName;
                string location = path + mapName;
                tabPage.UseVisualStyleBackColor = true;
                tabs.Add(tabPage);
                PictureBox pb = new PictureBox();
                pb.Name = "pictureBox" + pn;
                pb.Image = Image.FromFile(location);
                tabControl2.Controls.Add(tabPage);
                pb.Width = pb.Image.Width;
                pb.Height = pb.Image.Height;
                tabPage.Controls.Add(pb);
                currentComponent = tabPage;
                tabPage.Width = pb.Width;
                tabPage.Height = pb.Height;
                pn++;
                tabControl2.Width = pb.Width;
                tabControl2.Height = pb.Height;
                bmp1 = (Bitmap)pb.Image;
                int lx, ly;
                float realWidth = gpsD.Bounds[1] - gpsD.Bounds[3];
                float imageW = pb.Image.Width;
                float dx = imageW * (gpsD.Bounds[1] - gpsD.getWayPoint(0).Longitude) / realWidth;
                float realHeight = gpsD.Bounds[0] - gpsD.Bounds[2];
                float imageH = pb.Image.Height;
                float dy = imageH * (gpsD.Bounds[0] - gpsD.getWayPoint(0).Latitude) / realHeight;
                lx = (int)dx;
                ly = (int)dy;

                using (Graphics g = Graphics.FromImage(bmp1))
                {
                    Rectangle rect = new Rectangle(lx, ly, 20, 20);
                    if (gpsD.getWayPoint(0).Sym.Equals(""))
                    {
                        g.DrawRectangle(new Pen(Color.Red), rect);
                    }
                    else
                    {
                        if (gpsD.getWayPoint(0).Sym.Equals("hospital"))
                        {
                            g.DrawImage(symbolImages[0], rect);
                        }
                        else
                        {
                            if (gpsD.getWayPoint(0).Sym.Equals("university"))
                            {
                                g.DrawImage(symbolImages[1], rect);
                            }
                        }
                    }
                }
                pb.Image = bmp1;
                pb.Invalidate();

            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {

            string wayName = nameTxtBox.Text;

            double lat = Convert.ToDouble( latTxtBox.Text);
            float wayLat = (float)lat;


            double lon = Convert.ToDouble( longTxtBox.Text);
            float wayLong = (float)lon;


            double ele = Convert.ToDouble(elevTxtBox.Text);
            float wayEle = (float)ele;

            WayPoint wp = new WayPoint(wayName, wayLat, wayLong, wayEle);

            GPSDataPoint.Add(wp);
         }




        private void lb_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            MessageBox.Show(lb.SelectedIndex.ToString());
        }
    }
}

GPSDataPoint.cs

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

namespace CourseworkExample
{
    public class GPSDataPoint
    {
        private float[] bounds;
        private List<WayPoint> dataList;
        public GPSDataPoint()
        {
            dataList = new List<WayPoint>();
        }
        internal void setBounds(string p)
        {
            string[] b = p.Split(',');
            bounds = new float[b.Length];
            for (int i = 0; i < b.Length; i++)
            {
                bounds[i] = Convert.ToSingle(b[i]);
            }
        }
        public float[] Bounds { get { return bounds; } }
        internal void addWaypoint(string s)
        {
            WayPoint wp = new WayPoint(s);
            dataList.Add(wp);
        }
        public WayPoint getWayPoint(int i)
        {
            if (i < dataList.Count)
            {
                return dataList[i];
            }
            else
                return null;
        }
        public List<WayPoint> DataList { get { return dataList; } }

        internal void Add(WayPoint wp)
        {
          //  dataList.Add(wp);
        }
    }
}

WayPoint.cs

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

namespace CourseworkExample
{
    public class WayPoint
    {
        private string name;
        private float ele;
        private float latitude;
        private float longitude;
        private string sym;


        public WayPoint(string name, float latitude, float longitude, float elevation)
        {
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
            this.ele = elevation;
        }

        public WayPoint()
        {
            name = "no name";
            ele = 3.5F;
            latitude = 3.5F;
            longitude = 0.0F;
            sym = "no symbol";

        }

        public WayPoint(string s)
        {
            string[] bits = s.Split(',');
            name = bits[0];
            longitude = Convert.ToSingle(bits[2]);
            latitude = Convert.ToSingle(bits[1]);
            if (bits.Length > 4)
                sym = bits[4];
            else
                sym = "";
            try
            {
                ele = Convert.ToSingle(bits[3]);
            }
            catch (Exception e)
            {
                ele = 0.0f;
            }
        }

        public float Longitude
        {
            get { return longitude; }

            set { longitude = value; }
        }

        public float Latitude 
        {
            get { return latitude; }
            set { latitude = value; }

        }

        public float Ele 
        {   
            get { return ele; }
            set { ele = value; }
        }
        public string Name 
        {

            get { return name; }
            set { name = value; }

        }

        public string Sym
        {   
            get { return sym; }
            set { sym = value; }        
        }
    }
}

此外,.csv文件看起来像

birthplace.png 51.483788,-0.351906,51.460745,-0.302982 生于这里,51.473805,-0.32532, - ,医院 生于这里,51.473805,-0.32532, - ,医院

每当我想使用GPSDataPoint.Add(wp)时,我都会收到错误;我一定是犯了很傻的错,但我看不出来。请帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

您需要更改以下功能:

private void addBtn_Click(object sender, EventArgs e)
    {

        string wayName = nameTxtBox.Text;

        double lat = Convert.ToDouble( latTxtBox.Text);
        float wayLat = (float)lat;


        double lon = Convert.ToDouble( longTxtBox.Text);
        float wayLong = (float)lon;


        double ele = Convert.ToDouble(elevTxtBox.Text);
        float wayEle = (float)ele;

        WayPoint wp = new WayPoint(wayName, wayLat, wayLong, wayEle);

        GPSDataPoint gdp = new GPSDataPoint();
        gdp.Add(wp);

        //Do something with your gdp variable or use the public on you have declared instead
        //gpsdp.Add(wp);
     }