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...
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);
}
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)
{
m.RemoveCity(c1);
}
else
{
MessageBox.Show("abc");
}
}
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;
}