winforms - How to center a bunch of controls programmatically in c# -


i'm pretty new community, , have app adds controls programmatically. center of added controls sort of selecting them , pressing center on visual studio. , no don't want center each 1 aside. here's code used controls:

    private void getallcontrol(control c, list<control> list)     {         //gets controls , saves them list         foreach (control control in c.controls)         {             list.add(control);         }     }      //and call          list<control> list = new list<control>();         getallcontrol(pnl_custom, list);         foreach (play_panel m in list)         {             //and here want insert center code         } 

thanks in advance,

vbtheory

here's how center controls group. it's same before except compute how far center of mass group has move become center of parent control. iterate on controls , offset locations much. centers them while maintaining positions relative each other:

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         list<control> list = new list<control>();         getallcontrol(pnl_custom, list);         centercontrolsasgroup(list, direction.both); // center group in center of parent     }      public enum direction     {         vertical,         horizontal,         both     }      private void centercontrolsasgroup(list<control> controls, direction direction)     {         if (controls.count > 1)         {             int xsum = 0;             int ysum = 0;             point center;             foreach (control ctl in controls)             {                 center = new point(ctl.location.x + ctl.width / 2, ctl.location.y + ctl.height / 2);                 xsum = xsum + center.x;                 ysum = ysum + center.y;             }             point average = new point(xsum / controls.count, ysum / controls.count);              center = new point(controls[0].parent.width / 2, controls[0].parent.height / 2);             int xoffset = center.x - average.x;             int yoffset = center.y - average.y;              foreach (control ctl in controls)             {                 switch (direction)                 {                     case direction.vertical:                         ctl.location = new point(ctl.location.x + xoffset, ctl.location.y);                         break;                      case direction.horizontal:                         ctl.location = new point(ctl.location.x, ctl.location.y + yoffset);                         break;                      case direction.both:                         ctl.location = new point(ctl.location.x + xoffset, ctl.location.y + yoffset);                         break;                 }             }         }     }      private void getallcontrol(control c, list<control> list)     {         //gets controls , saves them list         foreach (control control in c.controls)         {             list.add(control);         }     }  } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -