(wpf c#)ive been trying to figure out how to add a line that starts at the button clicked and ends at the mouse pos on mouse move, losing will to live

Joined
Oct 16, 2022
Messages
1
Reaction score
0
C#:
        UserControl? movingObject;
        Point offset;
        Line l = new Line();

        bool makingConnection;
        private void X_PreviewMouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                movingObject = sender as UserControl;
                offset = e.GetPosition(canvas);
                offset.Y -= Canvas.GetTop(movingObject);
                offset.X -= Canvas.GetLeft(movingObject);
                canvas.CaptureMouse();
                Mouse.OverrideCursor = new Cursor(@"C:\Users\conra\source\repos\WpfApp1\WpfApp1\\grabbing.cur");
            }
            parentControl pc = (parentControl)sender;
            foreach(connector c in pc.connectors)
            {
                if (c.isClicked)
                {
                    makingConnection = true;
                    l.X1 = e.GetPosition(canvas).X;
                    l.Y1 = e.GetPosition(canvas).Y;
                }
            }
        }
        private void canvas_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (movingObject == null) return;
            Point pp = e.GetPosition(sender as IInputElement);
            Canvas.SetTop(movingObject, pp.Y - offset.Y);
            Canvas.SetLeft(movingObject, pp.X - offset.X);

            if (makingConnection)
            {
                l.Stroke = Brushes.Black;
                l.StrokeThickness = 4;
                l.X2 = e.GetPosition(sender as IInputElement).X;
                l.Y2 = e.GetPosition(sender as IInputElement).Y;
                canvas.Children.Add(l);
            }
        }
        private void canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            movingObject = null;
            canvas.ReleaseMouseCapture();
            Mouse.OverrideCursor = Cursors.Hand;
        }

this is what ive got so far, it doesnt add a line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top