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

  • SEARCH
  • Home
  • 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 4060732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:22:33+00:00 2026-05-20T15:22:33+00:00

I want to drag and drop that I have drawn on the forms. Here

  • 0

I want to drag and drop that I have drawn on the forms. Here is my code for drawing the rectangle. An this works fine.

        Rectangle rec = new Rectangle(0, 0, 0, 0);

        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Aquamarine, rec);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rec = new Rectangle(e.X, e.Y, 0, 0);
                Invalidate();
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rec.Width = e.X - rec.X;
                rec.Height = e.Y - rec.Y;
                Invalidate();
            }
        }

Now I want to drag and drop that rectangle in to a different place.

Pls help How to do that

Thank You

yohan

  • 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-20T15:22:34+00:00Added an answer on May 20, 2026 at 3:22 pm

    I have written a helper class for this kind of stuff:

    class ControlMover
    {
        public enum Direction
        {
            Any,
            Horizontal,
            Vertical
        }
    
        public static void Init(Control control)
        {
            Init(control, Direction.Any);
        }
    
        public static void Init(Control control, Direction direction)
        {
            Init(control, control, direction);
        }
    
        public static void Init(Control control, Control container, Direction direction)
        {
            bool Dragging = false;
            Point DragStart = Point.Empty;
            control.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                Dragging = true;
                DragStart = new Point(e.X, e.Y);
                control.Capture = true;
            };
            control.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                Dragging = false;
                control.Capture = false;
            };
            control.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (Dragging)
                {
                    if (direction != Direction.Vertical)
                        container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                    if (direction != Direction.Horizontal)
                        container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                }
            };
        }
    }  
    

    Then I just Initialize it in my form load event with my control:

    ControlMover.Init(myControl, myContainer, ControlMover.Direction.Any);  
    

    Well, you don’t have a control to move. It’s a rectangle. But hopefully, you’ll get the idea.
    UPDATE: Have you checked out the related questions listed in this page? Try:
    Drag and drop rectangle in C#

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

Sidebar

Related Questions

I currently have this code I've pieced together to accomplish drag and drop div
I have this Drag Drop UI, here , created from this tutorial http://net.tutsplus.com/tutorials/javascript-ajax/inettuts/ I
I want to do a simple drag-drop using jQuery. I have not done anything
I want to drag&drop from treeview to datagrid view. The code for drag is
I want to drag and drop a file so that the textbox shows the
I want to enable drag-and-drop behaviour on my Web application. I have an image
Let me start by saying that I have been reading the drag'n drop tutorial
I have a WPF 4 app which I want to enable drag and drop
I'm new to drag-and-drop in Swing. I have a JPanel that draws an image
I have a drag and drop calendar that 'on drop' needs to pass the

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.