Saya menambahkan Metode IsClicked (bool) ke dalam daftar. Pada saat yang sama, saya ingin memastikan Metode IsClicked(x,y) tidak sama dari daftar.

  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();
  }

Tampilkan Pesan jika keduanya sama.

c#
-2
Zrrr 8 Agustus 2019, 12:56

1 menjawab

Jawaban Terbaik

Saya sarankan periksa dulu apakah ada plant di plantsList dan baru kemudian Add:

  ...

  // 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();
1
Dmitry Bychenko 8 Agustus 2019, 10:04