C# 检查位置是否与列表中的相同
我将IsClicked方法(bool)添加到列表中。同时,我想确保IsClicked方法(x,y)与列表中的不同C# 检查位置是否与列表中的相同,c#,C#,我将IsClicked方法(bool)添加到列表中。同时,我想确保IsClicked方法(x,y)与列表中的不同 if (CheckInput()) { string name = textBoxName.Text; int size = Convert.ToInt32(textBoxSize.Text); decimal price = Convert.ToDecimal(textBoxPrice.Text); int x = e.X; int y
if (CheckInput()) {
string name = textBoxName.Text;
int size = Convert.ToInt32(textBoxSize.Text);
decimal price = Convert.ToDecimal(textBoxPrice.Text);
int x = e.X;
int y = e.Y;
Plant plant = new Plant(name, size, price, x, y);
plantsList.Add(plant);
totalCost =plantsList.Sum(item => item.Price);
if(plantsList.Any(n => n.IsClicked(x, y) ==
plant.IsClicked(x, y)))
{
MessageBox.Show("You draw at the same position");
}
pictureBoxGarden.Refresh();
}
如果它们相同,则显示一条消息。我建议首先检查
工厂列表中是否有这样的工厂
,然后再添加:
...
// plant we are going to add
Plant plant = new Plant(name, size, price, x, y);
// Do we have such a plant in the list?
if (plantsList.Any(item => item.IsClicked(x, y) == plant.IsClicked(x, y)))
{
MessageBox.Show("You draw at the same position");
}
else
{
// if no, let's add it and compute the new lotal cost
plantsList.Add(plant);
totalCost = plantsList.Sum(item => item.Price);
}
pictureBoxGarden.Refresh();
请看一下公共类Plant{private intx;private inty;public bool IsClicked(int x,int y){return(x-x)*(x-x)+(y-y)*(y-y)这是使用该方法的类。您的问题是什么?您期望得到什么,得到什么?我的问题是消息框会一直说每次都是“你在同一个位置画画”,尽管我不是。