Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 716675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:19:34+00:00 2026-05-14T05:19:34+00:00

I am trying to handle a drag & drop interaction, which involves mouse down,

  • 0

I am trying to handle a drag & drop interaction, which involves mouse down, mouse move, and mouse up.

Here is a simplified repro of my solution that:

  • on mouse down, creates an ellipse and adds it to a canvas
  • on mouse move, repositions the ellipse to follow the mouse
  • on mouse up, changes the colour of the canvas so that it’s obvious which one you’re dragging.

    var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(canvas, "MouseLeftButtonDown");
    var mouseUp = Observable.FromEvent<MouseButtonEventArgs>(canvas, "MouseLeftButtonUp");
    var mouseMove = Observable.FromEvent<MouseEventArgs>(canvas, "MouseMove");
    
    Ellipse ellipse = null;
    
    var q = from start in mouseDown.Do(x =>
                {
                    // handle mousedown by creating a red ellipse, 
                    // adding it to the canvas at the right position
                    ellipse = new Ellipse() { Width = 10, Height = 10, Fill = Brushes.Red };
                    Point position = x.EventArgs.GetPosition(canvas);
                    Canvas.SetLeft(ellipse, position.X);
                    Canvas.SetTop(ellipse, position.Y);
                    canvas.Children.Add(ellipse);
                })
            from delta in mouseMove.Until(mouseUp.Do(x =>
                {
                    // handle mouse up by making the ellipse green
                    ellipse.Fill = Brushes.Green;
                }))
            select delta;
    
    q.Subscribe(x =>
    {
        // handle mouse move by repositioning ellipse
        Point position = x.EventArgs.GetPosition(canvas);
        Canvas.SetLeft(ellipse, position.X);
        Canvas.SetTop(ellipse, position.Y);
    });
    

the XAML is simply

    <Canvas x:Name="canvas"/>

There’s a few things I don’t like about this code, and I need help refactoring it 🙂

First of all: the mousedown and mouseup callbacks are specified as side effects. If two subscriptions are made to q, they will happen twice.

Second, the mouseup callback is specified before the mousemove callback. This makes it a bit hard to read.

Thirdly, the reference to the ellipse seems to be in a silly place. If there’s two subscriptions, that variable reference will get overwritten quite quickly. I’m sure that there should be some way we can leverage the let keyword to introduce a variable to the linq expression that will mean the correct ellipse reference is available to both the mouse move and mouse up handlers

How would you write this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:19:34+00:00Added an answer on May 14, 2026 at 5:19 am

    To avoid subscrition side-effects, you should publish your observable. I think something like this would be Ok:

            public MainWindow()
        {
            InitializeComponent();
            var mouseDown = Observable
                .FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonDown");
            var mouseUp = Observable
                .FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonUp");
            var mouseMove = Observable
                .FromEvent<MouseEventArgs>(this, "MouseMove");
    
            var ellipses = mouseDown
                .Select(args => new { 
                    a = args, 
                    el = new Ellipse
                    {
                        Width = 10, Height = 10, Fill = Brushes.Red
                    }})
                .Publish();
    
            ellipses
                .Subscribe(elargs =>
                {
                    var position = elargs.a.EventArgs.GetPosition(canvas);
                    Canvas.SetLeft(elargs.el, position.X);
                    Canvas.SetTop(elargs.el, position.Y);
                    canvas.Children.Add(elargs.el);
                });
    
            var elmove = from elargs in ellipses
                         from mm in mouseMove.TakeUntil(mouseUp)
                         select new { a = mm, el = elargs.el };
    
            elmove.
                Subscribe(elargs =>
                {
                    var position = elargs.a.EventArgs.GetPosition(canvas);
                    Canvas.SetLeft(elargs.el, position.X);
                    Canvas.SetTop(elargs.el, position.Y);
                });
    
            var elmup = from elargs in ellipses
                        from mup in mouseUp
                        select elargs.el;
    
            elmup.Subscribe(el => el.Fill = Brushes.Green);
    
            ellipses.Connect();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write test harness for part of my Android mapping application. I
I'm trying to build a C++ extension for python using swig. I've followed the
I'm trying to build a Chrome browser extension, that should enhance the way the
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
I am trying to load a html page through UIWebview.I need to disable all
I am trying to understand the practical difference during the execution of a program
I am playing with TFS 2010, and am trying to setup a build process
I have several USB mass storage flash drives connected to a Ubuntu Linux computer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.