winforms - C# drag controls around a panel -


i developing system allow user drag objects around within same panel, went through research , founds should use mouse events mouse_up, mouse_down , mouse_move.

the program generate 3 picturebox , allow user drag around every picturebox within panel, program code did not work when drag on picturebox, picturebox move, not according mouse cursor location, somewhere else, besides, when dragging, there picturebox shadows in panel, i've tried update(),refresh(), , invalidate() seems not useful me. below codes, helping

public partial class form1 : form {      list<picturebox> pictureboxlist = new list<picturebox>();     private bool isdragging = false;      public form1()     {         initializecomponent();          (int = 0; < 3; i++)         {             picturebox picture = new picturebox             {                 name = "picturebox" + i,                 size = new size(20, 20),                 location = new point(i * 40, * 40),                 borderstyle = borderstyle.fixedsingle,                 sizemode = pictureboxsizemode.zoom,                 imagelocation = "a.jpg"             };             pictureboxlist.add(picture);               foreach (picturebox p in pictureboxlist)             {                 p.mousedown += new mouseeventhandler(c_mousedown);                 p.mousemove += new mouseeventhandler(c_mousemove);                 p.mouseup += new mouseeventhandler(c_mouseup);                 pnldisplayimage.controls.add(p);                 pnldisplayimage.refresh();             }         }     }       void c_mousedown(object sender, mouseeventargs e)     {         isdragging = true;     }      void c_mousemove(object sender, mouseeventargs e)     {          if (isdragging == true) {             control c = sender control;             (int = 0; < pictureboxlist.count(); i++)             {                 if (c.equals(pictureboxlist[i]))                 {                     pictureboxlist[i].location = new point(e.x, e.y);                 }             }         }     }      void c_mouseup(object sender, mouseeventargs e)     {         picturebox c = sender picturebox;         isdragging = false;         (int = 0; < pictureboxlist.count(); i++) {              if (c.equals(pictureboxlist[i])){                 pictureboxlist[i].location = new point(e.x, e.y);             }         }     }      private void pnldisplayimage_paint(object sender, painteventargs e)     {         foreach (picturebox p in pictureboxlist)         {             pnldisplayimage.controls.add(p);         }     }  } 

finally i've found problems caused program not running expectations. main problem accidentally put foreach loop inside loop used create picturebox, problem caused picturebox comes out shadows effect while dragging during run time due there few same picturebox. also, have change little bit of codes , run expected. below code want answer.

public partial class form1 : form {      list<picturebox> pictureboxlist = new list<picturebox>();     private bool isdragging = false;     point move;      public form1()     {         initializecomponent();          (int = 0; < 3; i++)         {             picturebox picture = new picturebox             {                 name = "picturebox" + i,                 size = new size(20, 20),                 location = new point(i * 40, * 40),                 borderstyle = borderstyle.fixedsingle,                 sizemode = pictureboxsizemode.zoom,                 imagelocation = "a.jpg"             };             pictureboxlist.add(picture);         }          foreach (picturebox p in pictureboxlist)         {                 p.mousedown += new mouseeventhandler(c_mousedown);                 p.mousemove += new mouseeventhandler(c_mousemove);                 p.mouseup += new mouseeventhandler(c_mouseup);                 pnldisplayimage.controls.add(p);                 pnldisplayimage.refresh();          }      }      void c_mousedown(object sender, mouseeventargs e)     {         control c = sender control;         isdragging = true;         move = e.location;     }      void c_mousemove(object sender, mouseeventargs e)     {          if (isdragging == true) {             control c = sender control;             (int = 0; < pictureboxlist.count(); i++)             {                 if (c.equals(pictureboxlist[i]))                 {                     pictureboxlist[i].left += e.x - move.x;                     pictureboxlist[i].top += e.y - move.y;                 }             }         }     }      void c_mouseup(object sender, mouseeventargs e)     {         isdragging = false;     } } 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -