www.softwareriviera.com
Software Code
    New Code     Most Interesting     Don't Miss     Our picks  
C# - section

View Article
Draggable Form in C#
Create a draggable form in C#. A draggable form means that you can grab it from anywhere inside the content area and drag it around the screen. There is one requirement: there shouldn't be control on the area where the dragging takes place.
The code below shows you have you can create a draggable form in C#. A draggable form means that you can grab it from anywhere inside the content area and drag it around the screen.

There is one requirement and that is that there shouldn't be any panels or other controls on the portion of the form where the grabbing takes place.

public class Form1 : Form
{
public bool _isDragging;
public int _startDraggingX;
public int _startDraggingY;

public Form1()
{
InitializeComponent();
_isDragging = false;
_startDraggingX = 0;
_startDraggingY = 0;
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
_startDraggingX = e.X;
_startDraggingY = e.Y;
_isDragging = true;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_isDragging = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (_isDragging)
{
int x = e.X + this.Left;
int y = e.Y + this.Top;
this.Top = y-_startDraggingY;
this.Left = x-_startDraggingX;
}
}
}
Date: 2012-5-26    Written by: Admin A
     See all articles in this section
Copyright © 2007-2009 SoftwareRiviera.com. All rights reserved.  | Terms of use | Disclaimer | Privacy Policy |