Question

So I have a bit of a problem with moving Ovalshapes around a form. The goal is to have a circle move within the bounds of two other circles, one placed inside the other and the moving circle essentially moving around them.

I am having trouble moving the circle with the mouse. Whenever I click and hold the circle, the circle moves to the coordinates of the location I clicked on the circle, such that if I click in the middle of an Ovalshape of size 10, it would set the circle's location to (5,5).

Here is what I have:

public partial class Form1 : Form
{

    int smallRadius;
    int largeRadius;
    int movingRadius;
    int distanceFromCenterToLocation;

    bool mouseDown;

    Point movingCenter;
    Point returnPoint;
    public Form1()
    {

        mouseDown = false;
        InitializeComponent();

        smallRadius = (ovalShape1.Right - ovalShape1.Left) / 2;

        largeRadius = (ovalShape2.Right - ovalShape2.Left) / 2;

        Point center = new Point(ovalShape1.Left + smallRadius, ovalShape1.Top + smallRadius);

        ovalShape3.Height = largeRadius - smallRadius;
        ovalShape3.Width = largeRadius - smallRadius;

        movingRadius = (ovalShape3.Right - ovalShape3.Left) / 2;

        ovalShape3.Location = new Point(center.X - (movingRadius), center.Y - largeRadius);

        movingCenter = new Point(ovalShape3.Left + movingRadius, ovalShape3.Top + movingRadius);

        distanceFromCenterToLocation = Convert.ToInt32(Math.Sqrt(Math.Pow(movingRadius, 2.0) + Math.Pow(movingRadius, 2.0)));

        int middleRadius = center.X - movingCenter.X;



    }

    private void ovalShape3_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;
    }

    private void ovalShape3_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDown = true;
    }

    private void ovalShape3_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown)
        {
            ovalShape3.Location = e.Location;
        }
    }

}
Was it helpful?

Solution

For some reason OvalShape isn't derived from Control and doesn't behave like a control.

  • When a control receives a mouse event the Location property of the MouseEventArgs holds the coordinates relative to the upper-left corner of the form. Shapes however receive the coordinates relative to their own upper-left corner.

  • When a mouse button is pressed over a control it will capture the mouse so that subsequent events are sent to the same control until you release the button. Shapes only receive mouse events if the mouse is over the shape no matter if a mouse button is pressed. Once you move the mouse to the top or left the shape doesn't receive any mouse events. Therefore the shape doesn't follow the mouse and also the MouseUp event is not handled.

All shapes in a form are embedded in a single control of type ShapeContainer. This is created automatically when you add a shape to a form in the Visual Studio designer. To get the expected behavior you need to find that ShapeContainer (probably called shapeContainer1) and handle its mouse events instead:

public Form1()
{
    InitializeComponent();

    // do your initialization
    ...

    // assign the events to the ShapeContainer
    // don't forget to remove the handlers from ovalShape3 in the designer!!!
    ShapeContainer container = ovalShape3.Parent;
    container.MouseUp += ovalShape3_MouseUp;
    container.MouseDown += ovalShape3_MouseDown;
    container.MouseMove += ovalShape3_MouseMove;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top