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 8718261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:29:59+00:00 2026-06-13T06:29:59+00:00

I basically have a simple problem in my program that I just want to

  • 0

I basically have a simple problem in my program that I just want to make sure goes right. It should on the click of the mouse button add the MouseEventHandler and then move the circle along with the mouse until the event handler gets removed. I simplified the code to the very basics:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid Name="grid1" Background="White" MouseLeftButtonUp="grid_MouseUp">
        <Ellipse Height="50" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ellipse1" Stroke="{x:Null}" VerticalAlignment="Top" Width="50" Fill="Black" MouseLeftButtonDown="ellipse1_MouseDown" />
    </Grid>
</Window>

C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private static Point _oldPoint = new Point(), _newPoint = new Point();

    private void ellipse1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        _oldPoint = e.GetPosition(grid1);
        grid1.MouseMove += new MouseEventHandler(grid_MouseMove);
    }

    private void grid_MouseUp(object sender, MouseButtonEventArgs e)
    {
        grid1.MouseMove -= new MouseEventHandler(grid_MouseMove);
    }

    private void grid_MouseMove(object sender, MouseEventArgs e)
    {
        _newPoint = e.GetPosition(grid1);
        ellipse1.Margin = new Thickness(ellipse1.Margin.Left - _oldPoint.X + _newPoint.X, ellipse1.Margin.Top - _oldPoint.Y + _newPoint.Y, 0, 0);
        _oldPoint = _newPoint;
    }
}

Now in general this code works fine and I think is quite neat as it doesn’t check the movement of the mouse until one actually presses the button. However, my question is as follows:

I had to add the MouseMove event to the grid rather than to the circle, because once the mouse pointer loses focus of the circle (by moving the mouse too fast) it doesn’t trigger the MouseMove event anymore. But why exactly does that happen? At the beginning of the event the mouse was definitely above the circle and then it moved. Yes, it moved away from the circle but shouldn’t that still trigger the event?

  • 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-06-13T06:30:00+00:00Added an answer on June 13, 2026 at 6:30 am

    You can capture the mouse and handle all events in your ellipse.

    <Grid Name="grid1" Background="White">
        <Ellipse Height="50" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ellipse1" Stroke="{x:Null}" VerticalAlignment="Top" Width="50" Fill="Black"
                 MouseLeftButtonDown="ellipse1_MouseDown" MouseLeftButtonUp="ellipse1_MouseUp" />
    </Grid>
    

    with this code behind

    private void ellipse1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Mouse.Capture(ellipse1);
        _oldPoint = e.GetPosition(grid1);
        ellipse1.MouseMove += new MouseEventHandler(ellipse1_MouseMove);
    }
    
    private void ellipse1_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Mouse.Capture(null);
        ellipse1.MouseMove -= new MouseEventHandler(ellipse1_MouseMove);
    }
    

    I’ve moved and renamed grid_MouseMove to ellipse1_MouseMove.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple problem. Basically I have an array called $list that
I have a simple controller called list_controller with an index that basically does def
I have a simple script that does some search and replace. This is basically
I have a really simple Rails app. Basically an article with comments. I want
I basically have a program that filters records from one excel file to another
I'm just playing around with a simple program that opens the camera. That's literally
I have this program that I want other processes to be able to call
Basically I have a simple login form. In the database I have a 'last_logged'
I have a simple application which basically consists of a line of buttons and
I'm at my wits end, and it's probably something really simple. Basically I've have

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.