Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Connection Works,But I Need To Remove A City From The Grid,The Problem Is When I Click The Remove Button And Choose A City,All...



C#
public partial class Form1 : Form
{

    List<City> cities = new List<City>();
    City city;
    Graphics gr;
    Map m;
    bool addingCity = false;
    bool removingCity = false;
    bool addingConnection = false;
    bool removeConnection = false;

    public int click1X, click1Y, click2X, click2Y;

    public Form1()
    {
        InitializeComponent();
        m = new Map();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        addingCity = true;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {

        if (addingCity)
        {
            gr = pictureBox1.CreateGraphics();
            Pen greenPen = new Pen(Color.Green, 3);
            SolidBrush greenBrush = new SolidBrush(Color.Green);

            city = new City(textBox1.Text, e.X, e.Y, greenPen);
            if (textBox1.Text == "")
            {
                MessageBox.Show("You must give a name to the city!");
            }
            else if (m.AddCity(textBox1.Text, e.X, e.Y, greenPen))
            {
                Font myFont = new Font("Verdana", 12F, FontStyle.Bold);
                foreach (City c in m.Cities)
                {
                    gr.DrawString(city.Id, myFont, Brushes.Red, e.X + 10, e.Y - 20);
                }

                city.Draw(gr, greenPen, greenBrush);
                //MessageBox.Show("X: "+city.X+" Y: "+city.Y);
            }
            else
            {
                MessageBox.Show("The ciy " + city.Id + " is already on the map! Or There is already a city on that place!");
            }



            button1.Enabled = true;
            addingCity = false;
        }

        else if (removingCity)
        {
            click1X = e.X;
            click1Y = e.Y;
            City c1 = m.GetCity(click1X, click1Y);
            if (c1 != null)
            {
                //MessageBox.Show("abc");
                m.RemoveCity(c1);
               //pictureBox1.Refresh();
            }
            else
            {
               // pictureBox1.Refresh();
               MessageBox.Show("abc");
            }

        }

C#
public bool RemoveCity(City c)
       {
           foreach (City ct in cities)
           {
               if (GetCity(ct.X,ct.Y) != null)
               {
                   this.cities.Remove( c);
                   return true;
               }
           }
           return false;
       }
Posted
Updated 28-Mar-14 18:17pm
v3
Comments
Your description is incomplete. Don't write it in title of question. Write in question body instead. Briefly describe the problem in title field.
[no name] 28-Mar-14 23:04pm    
Yes Exactly You need complete your description very well
[no name] 29-Mar-14 9:30am    
Well the answer is really very simple, all you need to do is
CHill60 29-Mar-14 12:11pm    
rotflmao!

1 solution

It is very unlikely that the coordinates obtained in the MouseDown event handler in removingCity will exactly match the coordinates provided in addingCity. You need to test a circular region around a given point rather than test the point.
 
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