winforms - How to create moveable (by mouse) buttons at runtime c# -


i using following code create buttons @ run-time how make them movable (drag-able mouse on screen anywhere)

var b = new button();             b.text = "my button";             b.name= "button";              b.click += new eventhandler(b_click);             b.mouseup += new mouseeventhandler(this.b_mouseup);             b.mousedown += new mouseeventhandler(this.b_mousedown);             b.mousemove += new mouseeventhandler(this.b_mousemove);             this.mypanel.controls.add(b); 

i have tried work mouse events not make them move according mouse pointer

since mouse can move outside button while dragging, have use control.capture property.
sample let's move buttons not on entire screen, within bounds of parent container (or outside it, hidden, should prevented).

    private point origin_cursor;     private point origin_control;     private bool btndragging = false;      private void button1_click(object sender, eventargs e)     {         var b = new button();         b.text = "my button";         b.name = "button";         //b.click += new eventhandler(b_click);         b.mouseup += (s, e2) => { this.btndragging = false; };         b.mousedown += new mouseeventhandler(this.b_mousedown);         b.mousemove += new mouseeventhandler(this.b_mousemove);         this.panel1.controls.add(b);     }      private void b_mousedown(object sender, mouseeventargs e)     {         button ct = sender button;         ct.capture = true;         this.origin_cursor = system.windows.forms.cursor.position;         this.origin_control = ct.location;         this.btndragging = true;     }      private void b_mousemove(object sender, mouseeventargs e)     {         if(this.btndragging)         {             button ct = sender button;             ct.left = this.origin_control.x - (this.origin_cursor.x - cursor.position.x);             ct.top = this.origin_control.y - (this.origin_cursor.y - cursor.position.y);         }     } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -