Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys!
In that case I have one list with the cities. My question is how I can select 2 cities and draw a line between them? Should I use a method like GetCity(xmouse, ymouse) or something else? Please guys help me!

C#
public partial class Form1 : Form
    {
        List<city> cities = new List<city>();
        City city;
        Graphics gr;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
 
        }
 
       
 
       
 
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
           
            
            gr = pictureBox1.CreateGraphics();
            Pen greenPen = new Pen(Color.Green, 3);
            city = new City(textBox1.Text, e.X, e.Y, greenPen);
            cities.Add(city);
            Font myFont = new Font("Verdana", 14F, FontStyle.Bold);
            foreach (City c in cities)
            {
                gr.DrawString(city.Id, myFont, Brushes.Red, e.X + 20, e.Y - 20);
            }
 
            gr.DrawEllipse(greenPen, e.X-10,e.Y-10,20,20);
            gr.FillEllipse(Brushes.Green, e.X-10, e.Y-10, 20, 20);
            
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
 
            listBox1.Items.Clear();
            foreach (City c in cities)
            {
                listBox1.Items.Add(c.AsString());
            }
        }
 
      
 
        private void button2_Click(object sender, EventArgs e)
        {
           /* Pen greenPen = new Pen(Color.Green, 3);
            foreach (City ct in cities)
            {
                gr.DrawLines(cities);
            }*/
 
            //Point[] citiesp = new Point[cities.Count];

           
            //Pen greenPen = new Pen(Color.Green, 3);
            //Point pt1 = new Point(city.X, city.Y);
            //Point pt2 = new Point(20,30);
            //Point[] pcities = new Point[cities.Count];
            //int i = 0;
            //foreach (City ct in cities)
            //{
            //    pcities[i++] = new Point(ct.X, ct.Y);
            //}
                    
            //gr.DrawLines(greenPen, pcities);
        }
    }

<img src='http://s23.postimg.org/vr16okjg7/help.jpg' border='0' alt="help" />
Posted
Updated 27-Mar-14 23:39pm
v3

Exactly how you do it depends on you, and what you have put in your City class - but I notice that the City constructor accepts and X and Y position:
C#
city = new City(textBox1.Text, e.X, e.Y, greenPen);
So you could just have a public Location property in the City class:
C#
public Point Location ( get; private set; }
And use that when you want to draw the line between them.
All you have to do in your constructor is:
C#
Location = new Point(x, y);

And drawing the line becomes trivial:
C#
gr.DrawLine(greenPen, firstCity.Location, secondCity.Location);



"No I want to get just the coordinates(x,y) from the objects in the combobox.
C#
private void button2_Click(object sender, EventArgs e)
        {
            
            foreach (City c in m.Cities)
            {
                listBox1.Items.Add(c.AsString());
                comboBox1.Items.Add(c.Id);
                comboBox2.Items.Add(c.Id);
            }
            m.Cities.Clear();
            
        }
 
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
           //City citycombo = comboBox1.SelectedItem as City;
            //City ct = new City("Sofia", 50, 50, Pens.Black);
            comboBox2.Items.Remove(comboBox1.SelectedItem);
            //label1.Text = "Starting point: " + ct.Id;
        }
 
        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Remove(comboBox2.SelectedItem);
            label2.Text = "End point: " + comboBox2.SelectedItem.ToString();
        }


I tried the commented code but it said that I have no reference to an object."



OK...there are some oddities there...
Why are you removing the selected item from the combobox each time the selection changes? Surely the Combobox is going to run out of items rather quickly? Shouldn't you be putting the "old" items back in?

Now, to your problem - and it's both simpler to fix than you think, and a little more complex! :laugh:

I want you to change your City class, and add an override method (Have you covered them yet?)
C#
public override string ToString()
    {
    return ...
    }
Now, remove the contents of your AsString method, and move them into ToString, replacing the return line I didn't complete. Almost certainly, it will now look something like this:
C#
public override string ToString()
    {
    return CityName;
    }
You can now get rid of the AsString method completely, (and if you use it in code you haven't shown here, change the name to ToString)
What happens now is that whenever C# wants to use your City instance as a string, it will automatically call ToString for you - which means that this code can change:
C#
private void button2_Click(object sender, EventArgs e)
        {
            
            foreach (City c in m.Cities)
            {
                listBox1.Items.Add(c);
                comboBox1.Items.Add(c);
                comboBox2.Items.Add(c);
            }
            m.Cities.Clear();
            
        }
What that does is get the system to use the name of the city in the textbox and the comboboxes, because they will automatically call ToString for you.
Now, when you fetch the SelectedValue from the combobox, you can cast it straight to the actual instance of the City:
C#
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
    {
    City citycombo = comboBox1.SelectedItem as City;
    if (citycombo != null)
        {
        comboBox2.Items.Remove(citycombo);
        label1.Text = "Starting point: " + citycombo;
        }
    }
And it should all start to work...
 
Share this answer
 
v2
Comments
Member 10314038 28-Mar-14 6:17am    
My problem is that I dont know how to get the secondCity
OriginalGriff 28-Mar-14 6:29am    
Well, you have created a list of them - cities - so what's the problem with getting the other one?
(Remember, I can't see your screen, or access you HDD, so you have to spell it out for me!)
Member 10314038 28-Mar-14 13:08pm    
Can I upload a zip file then?
OriginalGriff 28-Mar-14 13:22pm    
No - but I'm not going to wade through *that* much code anyway!
I only need to know what you are trying to do, and why it's giving you a problem - I don't need your whole project! :laugh:
I need some detail - but the actual relevant code is going to be about the same size as the code you initially posted.
Member 10314038 28-Mar-14 6:40am    
can u go on google+ please
Yes, you have to implement a method like that, namely GetCity(xmouse, ymouse). It is a pretty straightforward task: you have to find the city most close to <code>{xmouse, ymouse} and pick it (if it is enough close to). 'Close to' is measured by the distance (or the simpler squared-distance: {xmouse-xcity)*(xmouse-xcity)+(ymouse-ycity)*(ymouse-ycity) ).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900