This is a simple script which demonstates how to move a control (here I used button) by dragging it (without using builtin drag-drop features ;))


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int x = 0;
private int y = 0;
public Form1()
{
InitializeComponent();
}

private void button1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
button1.Left = (button1.Left + e.X) - x;
button1.Top = (button1.Top +e.Y)-y;
}
}
}
}

VN:F [1.1.6_502]
Rating: 5.0/5 (2 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Checkbox - Check All, None
  2. Find your lucky number in c#
  3. Monitor your filesystem for changes in real time in .NET
  4. Change attributes of items in a combobox
  5. MD5 encryption with c#