1

I am trying to add an ellipse to my grid from my mouse position. How can I define where the ellipse is drawn?

here is my mouse down event:

private void GridCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _sensorPoint = Mouse.GetPosition(GridCanvas);
    AddSensor();
}

And my AddSensor() method:

private void AddSensor()
{
    Ellipse sensorEllipse = new Ellipse();
    SolidColorBrush solidColorBrush = new SolidColorBrush();


    solidColorBrush.Color = Color.FromArgb(0, 0, 0, 0);
    sensorEllipse.Fill = solidColorBrush;
    sensorEllipse.StrokeThickness = 2;
    sensorEllipse.Stroke = Brushes.Black;
    sensorEllipse.Width = 10;
    sensorEllipse.Height = 10;

    GridCanvas.Children.Add(sensorEllipse);
}

How can I add sensorEllipse where the mouse is clicked on the canvas?

2
  • Check out this question. Commented Aug 16, 2012 at 6:47
  • The point from the mouse is of type Point, and I need to split these into two doubles for it to work with Canvas.SetLeft(). Do I need to do this? Commented Aug 16, 2012 at 6:50

1 Answer 1

1

You can set position with any of those methods:

private void AddSensor()
{
        Ellipse sensorEllipse = new Ellipse();
        SolidColorBrush solidColorBrush = new SolidColorBrush();

        solidColorBrush.Color = Color.FromArgb(0, 0, 0, 0);

        sensorEllipse.Fill = solidColorBrush;

        sensorEllipse.SetValue(Canvas.LeftProperty, _sensorPoint.X);
        sensorEllipse.SetValue(Canvas.TopProperty, _sensorPoint.Y);

        //Canvas.SetLeft(sensorEllipse, _sensorPoint.X);
        //Canvas.SetTop(sensorEllipse, _sensorPoint.Y);

        sensorEllipse.StrokeThickness = 2;
        sensorEllipse.Stroke = Brushes.Black;
        sensorEllipse.Width = 10;
        sensorEllipse.Height = 10;

        //adding event handler for right mouse down:
        sensorEllipse.MouseRightButtonDown += new MouseButtonEventHandler(sensorEllipse_MouseRightButtonDown);

        GridCanvas.Children.Add(sensorEllipse);
}

EDIT For adding/deleting ellipses check, which mouse button was pressed at GridCanvas_MouseLeftButtonDown

private void GridCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
  _sensorPoint = Mouse.GetPosition(GridCanvas);
  AddSensor();
}

Then add event handler for new ellipses:

    void sensorEllipse_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        Ellipse ellipse = sender as Ellipse;
        GridCanvas.Children.Remove(ellipse);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! How would I go about removing them from the mouse location?
Do you want to add ellipse when user holding mouse down and remove it when he releases mouse button?
I want to add an ellipse when user left clicks (which my code does) and remove it when the user right clicks on the ellipse.
Take a look at new code - added event handler for new ellipses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.