Return to Snippet

Revision: 61075
at November 26, 2012 21:23 by Joerilocker


Initial Code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;


namespace Gmap
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();


        }


        //calculate distance
        public void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            //var
            TextReader tr = new StreamReader("addreses.txt");
            GDirections gdr;
            string line;
            var x = DirectionsStatusCode.ZERO_RESULTS;
            while ((line = tr.ReadLine()) != null)
            {
                string adrs = tr.ReadLine();
                x = GMapProviders.GoogleMap.GetDirections(out gdr, textBox1.Text, adrs, false, false, false, true, true);
                if (x == DirectionsStatusCode.OK)
                {
                    dataGridView1.Rows.Add(line, gdr.Distance);
                }
                else
                {
                    dataGridView1.Rows.Add("Addres is unknown", "Please change addres");
                }
            }
            tr.Close();
        }

        //save and load
        private void button2_Click(object sender, EventArgs e)
        {


            if (button2.Text == "save")
            {
                TextWriter tw = new StreamWriter("addreses.txt", false);
                for (int i = 0; dataGridView1.RowCount > i; i++)
                {
                    for (int a = 0; a < 2; a++)
                    {
                        if (dataGridView1[a, i].Value != "" && dataGridView1[a, i].Value != null)
                        {
                            tw.WriteLine(Convert.ToString(dataGridView1[a, i].Value));
                        }
                    }
                }
                tw.Close();
                MessageBox.Show("data has been updated");
                button2.Text = "Edit adresses";
                dataGridView1.Rows.Clear();
                dataGridView1.Columns[1].HeaderText = "Distance";
                button1.Enabled = true;
                dataGridView1.ReadOnly = true;
                googleMapsToolStripMenuItem.Enabled = true;
            }

            else//Edit addreses
            {
                googleMapsToolStripMenuItem.Enabled = false;
                dataGridView1.Rows.Clear();
                dataGridView1.Columns[1].HeaderText = "Addres";
                button2.Text = "save";
                dataGridView1.ReadOnly = false;
                button1.Enabled = false;
                //load
                try
                {
                    TextReader tr = new StreamReader("addreses.txt");

                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        dataGridView1.Rows.Add(line, tr.ReadLine());
                    }
                    tr.Close();
                }
                catch { }
            }
        }

        //clear on click
        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "Addres")
                textBox1.Text = null;
        }

        //Delete row
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
            }
            catch
            { }
        }

        //on enter press
        private void enter(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1_Click(button1, e);
            }
        }


        private void googleMapsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string web;
            
                StreamReader sr = new StreamReader("addreses.txt");
                string line;

                while ((line = sr.ReadLine()) != dataGridView1[0, dataGridView1.CurrentRow.Index].Value)
                {
                    
                    if(line.Equals(dataGridView1[0, dataGridView1.CurrentRow.Index].Value) == true)
                    {
                        break;
                    }

                }

            web = sr.ReadLine();
            sr.Close();

            System.Diagnostics.Process.Start("https://maps.google.com/maps?q=" + web + "+to+" + textBox1.Text);
            
           
            
        }
    }
}
//Created by Joeri Lock

Initial URL


Initial Description
Application to calculate the distance between serveral adresses

Initial Title
Distance comparer

Initial Tags


Initial Language
C#