How to edit item in a listbox shown from reading a .csv file?

Posted by Shuvo on Stack Overflow See other posts from Stack Overflow or by Shuvo
Published on 2012-12-15T20:55:40Z Indexed on 2012/12/15 23:03 UTC
Read the original article Hit count: 292

Filed under:
|

I am working in a project where my application can open a .csv file and read data from it. The .csv file contains the latitude, longitude of places. The application reads data from the file shows it in a static map and display icon on the right places. The application can open multiple file at a time and it opens with a new tab every time. But I am having trouble in couple of cases

  1. When I am trying to add a new point to the .csv file opened. I am able to write new point on the same file instead adding a new point data to the existing its replacing others and writing the new point only.

  2. I cannot use selectedIndexChange event to perform edit option on the listbox and then save the file.

Any direction would be great.

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();
        string name = "";
        string path = "";
        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("\\");

                name = ofd.FileName;
                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;

                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("\\");

                name = ofd.FileName;
                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();
                MessageBox.Show(data.ToString());
            }

        }

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

        private void addBtn_Click(object sender, EventArgs e)
        {

            string wayName = nameTxtBox.Text;
            float wayLat = Convert.ToSingle(latTxtBox.Text);
            float wayLong = Convert.ToSingle(longTxtBox.Text);
            float wayEle = Convert.ToSingle(elevTxtBox.Text);
            WayPoint wp = new WayPoint(wayName, wayLat, wayLong, wayEle);
            GPSDataPoint gdp = new GPSDataPoint();
            data = new List<GPSDataPoint>();
            gdp.Add(wp);
            lb.Items.Add(wp.Name + " " + wp.Latitude + " " + wp.Longitude + " " + wp.Ele + " " + wp.Sym);
            lb.Refresh();
            StreamWriter sr = new StreamWriter(name);
            sr.Write(lb);
            sr.Close();

            DialogResult result = MessageBox.Show("Save in New File?","Save", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.FileName = "default.csv";
                DialogResult saveResult = saveDialog.ShowDialog();
                if (saveResult == DialogResult.OK)
                {
                    sr = new StreamWriter(saveDialog.FileName, true);
                    sr.WriteLine(wayName + "," + wayLat + "," + wayLong + "," + wayEle);
                    sr.Close();
                }

            }
            else
            {
               // sr = new StreamWriter(name, true);
               // sr.WriteLine(wayName + "," + wayLat + "," + wayLong + "," + wayEle);
                sr.Close();
            }
            MessageBox.Show(name + path);
         }
    }
}

GPSDataPoint.cs

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

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 file data

birthplace.png
51.483788,-0.351906,51.460745,-0.302982 
Born Here,51.473805,-0.32532,-,hospital
Danced here,51,483805,-0.32532,-,hospital

© Stack Overflow or respective owner

Related posts about c#

Related posts about csv