Home > Mobile >  Select an object inside a pictureBox with left mouse click
Select an object inside a pictureBox with left mouse click

Time:12-17

Goal is to select an object inside a pictureBox and change the values that made up that image/object.

I am just exploring how to select an object.

Each object has a series of arugments that will draw a flower

Flower newflower = new Flower(outerRadius, innerPart, innerColor, numberOfPetals, petalWidth, petalShape, petalColor, x, y);

Each object to placed in an object list

_gardenList = new List<Flower>();
_gardenList.Add(newflower);

The list of Flower objects are drawn to a pictureBox

foreach (Flower flower in _gardenList)
{
   newflower.Draw(flower);
}



// First draw the petals
  float x0 = _centreX - _petalWidth * _outerRadius;
  float y0 = _centreY - _outerRadius;
  float width = 2.0f * _petalWidth * _outerRadius;
  float height = _outerRadius;
  PointF centre = new PointF(_centreX, _centreY);
  Matrix matrix = new Matrix();
  float angle = 360.0f / _numberOfPetals;
  PointF[]? polygon = null;
  if (!_petalsRound) {
    polygon = new PointF[] { centre, new PointF(x0   width, y0), new PointF(x0, y0) };
  }
  for (int i = 0; i < _numberOfPetals; i  ) {
    paper.Transform = matrix;
    if (!_petalsRound) {
      Debug.Assert(polygon != null);
      paper.FillPolygon(_petalBrush, polygon);
    } else {
      paper.FillEllipse(_petalBrush, x0, y0, width, height);
    }
    matrix.RotateAt(angle, centre);
  }
  paper.ResetTransform();
  // Then draw a filled circle over the centre
  float innerRadius = _outerRadius * _innerPart;
  float innerDiameter = 2.0f * innerRadius;
  paper.FillEllipse(_innerBrush, _centreX - innerRadius, _centreY - innerRadius,
                    innerDiameter, innerDiameter);
}

Question is how do I select an object in the pictureBox so I can do something with it? I have selected a 'MouseClick' event, related to the pictureBox, and I know I can add the following code to that click event method.

 if (e.Button == MouseButtons.Left) 
  {
       // Do something
  }

But I do no know how to select a WHOLE object.

Thanks

CodePudding user response:

Did some research and iterate thought the object list using isClicked(x,y)

  // Read mouse-click position for x and y coordinates
  int x = e.X;
  int y = e.Y;

 if (e.Button == MouseButtons.Left) 
 {
     foreach (Flower flowerToChange in _gardenList) 
     {
         if (flowerToChange.IsClicked(x, y)) 
         {
             MessageBox.Show("We have clicked on an object flower");
         {
      }
  }
  • Related