Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 class Sensor:PictureBox
    {  public Sensor():base()
        {
            this.Size = new System.Drawing.Size(50, 50);
            this.Location = new System.Drawing.Point(50, 50);
            this.BackColor = Color.Red;
        }
}

In Form1.cs
C#
Sensor sn = new Sensor();
       public Form1()
       {
           InitializeComponent();
           this.Controls.Add(sn)
       }

this is my class i want Drag this sensor on Form . the code of this should be in the class itself Is that possible ? any shortest method ?
Posted
Updated 14-Feb-14 4:48am
v3
Comments
BillWoodruff 14-Feb-14 10:52am    
What have you tried so far ? Please show your code.

If you are just getting started on this, here's a hint: you will need to define MouseDown, MouseUp, and MouseMove EventHandlers for your PictureBox.

C#
 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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900