Visual C# Developer Center > Visual C# Forums > Visual C# Language > how to use drag and drop function?
Ask a questionAsk a question
 

Answerhow to use drag and drop function?

  • Saturday, November 07, 2009 12:40 PMNikesh2046 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    me and my frens are doing a project on a knapsack based game.
    so we need to use drag and drop function to do a particular task.
    so can anybody help us in using an function of drag and drop,like simply creating and object and drag that object in the form using mouse.

Answers

  • Saturday, November 07, 2009 12:47 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,

    You can develop your own custom control for this property.

    You need to be similar with OnPaint,MouseDown,MouseUp and MouseMove events.

    Here is a sample that draws a line on the form and allows user to move it at runtime.

        public class MyLine:Control
        {
    
            bool drawBold = false;
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Pen p = null;
                if (drawBold)
                {
                   p= new Pen(this.ForeColor, this.Height);
                }
                else
                {
                   p= new Pen(this.ForeColor, this.Height-5);
                }
                e.Graphics.DrawLine(p , 0, this.Height/2, this.Width, this.Height/2);
            }
    
            protected override void OnMouseHover(EventArgs e)
            {
                base.OnMouseHover(e);
                drawBold = true;
                this.Refresh();
            }
            protected override void OnMouseLeave(EventArgs e)
            {
                base.OnMouseLeave(e);
                drawBold = false;
                this.Refresh();
            }
    
            //to move control
            Point p = new Point();
            protected override void OnMouseDown(MouseEventArgs e)
            {
                base.OnMouseDown(e);
                p.X = e.X;
                p.Y = e.Y;
    
            }
            protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                if (e.Button == MouseButtons.Left)
                {
                    this.Location = new Point(e.X - p.X + this.Location.X, e.Y - p.Y + this.Location.Y);
    
                }
            }
        }
    

All Replies

  • Saturday, November 07, 2009 12:47 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,

    You can develop your own custom control for this property.

    You need to be similar with OnPaint,MouseDown,MouseUp and MouseMove events.

    Here is a sample that draws a line on the form and allows user to move it at runtime.

        public class MyLine:Control
        {
    
            bool drawBold = false;
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                Pen p = null;
                if (drawBold)
                {
                   p= new Pen(this.ForeColor, this.Height);
                }
                else
                {
                   p= new Pen(this.ForeColor, this.Height-5);
                }
                e.Graphics.DrawLine(p , 0, this.Height/2, this.Width, this.Height/2);
            }
    
            protected override void OnMouseHover(EventArgs e)
            {
                base.OnMouseHover(e);
                drawBold = true;
                this.Refresh();
            }
            protected override void OnMouseLeave(EventArgs e)
            {
                base.OnMouseLeave(e);
                drawBold = false;
                this.Refresh();
            }
    
            //to move control
            Point p = new Point();
            protected override void OnMouseDown(MouseEventArgs e)
            {
                base.OnMouseDown(e);
                p.X = e.X;
                p.Y = e.Y;
    
            }
            protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                if (e.Button == MouseButtons.Left)
                {
                    this.Location = new Point(e.X - p.X + this.Location.X, e.Y - p.Y + this.Location.Y);
    
                }
            }
        }