class Sensor:PictureBox
{
private Point init_mouse, Curnt_mouse,init_loc;
private bool IsDragable;
public Sensor():base()
{
this.Size = new System.Drawing.Size(50, 50);
this.Location = new System.Drawing.Point(50, 50);
this.BackColor = Color.Red;
this.MouseDown += new MouseEventHandler(Sensor_MouseDown);
this.MouseUp += new MouseEventHandler(Sensor_MouseUp);
this.MouseMove += new MouseEventHandler(Sensor_MouseMove);
IsDragable = false;
}
void Sensor_MouseMove(object sender, MouseEventArgs e)
{
if (IsDragable)
{
Curnt_mouse = Cursor.Position;
int dx = Curnt_mouse.X - init_mouse.X;
int dy = Curnt_mouse.Y - init_mouse.Y;
this.Location = new Point(init_loc.X + dx, init_loc.Y + dy);
}
}
void Sensor_MouseUp(object sender, MouseEventArgs e)
{
IsDragable = false;
}
void Sensor_MouseDown(object sender, MouseEventArgs e)
{
IsDragable = true;
init_mouse= Cursor.Position;
init_loc = this.Location;
}
}
}
tried this and worked
if any better option please suggest.