嗨,伙计们,我要打造一个应用程序来连接地图中的两个城市, [英] Hi,Guys,I Am Going To Make A App To Connect 2 Cities In A Map,
本文介绍了嗨,伙计们,我要打造一个应用程序来连接地图中的两个城市,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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...
< br $>
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");
}
}
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;
}
推荐答案
在removeCity的MouseDown事件处理程序中获得的坐标不太可能与提供的坐标完全匹配在加入城市。您需要测试给定点周围的圆形区域,而不是测试该点。
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.
这篇关于嗨,伙计们,我要打造一个应用程序来连接地图中的两个城市,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文