Array of controls

Movian

Member
Joined
Jul 29, 2013
Messages
8
Location
VA, USA
Programming Experience
3-5
Hey,
I am working on a project where i need to be able to create an unspecified number of controls (Calendars) on a form and dynamically size them based on the space on the form. I have most of this working however i am having trouble putting the the controls into an array... I have some sample code that will create an additional control without problem so i just need a little help tweaking it :)

(Please note i come from an Access/VBA background so please don't judge for not knowing some of the C# Nuances!)

C#:
int formsize = this.Width;
            int leftSpot = 0;
            calwidth = this.calendar1.Size.Width;
            leftmargin = formsize - calwidth;

            //resize existing calendar
            numCals = numCals + 1;
            calwidth = (calwidth / numCals) - 5;
            this.calendar1.Width = calwidth;

            //Create new calendar control
            Calendar Cal2 = new Calendar();
            

                Cal2.Name = "calendar" + numCals.ToString();
                Cal2.Width = calwidth;
                Cal2.Height = this.calendar1.Height;
                Cal2.Top = calendar1.Top;

                calwidth = (this.Width - this.monthView1.Width) - (5 * numCals) / numCals;
                calwidth = calwidth - (5 * numCals);
                calwidth = calwidth / numCals;


                Cal2.Left = this.monthView1.Width + (calwidth * (numCals -1));
                Cal2.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top );

            //Place control on form

            Controls.Add(Cal2);

            //relocate other calendars
            ReSizeCalendars();
 
Instead of all that nasty positioning and resizing code, use a TableLayoutPanel or FlowLayoutPanel. They will handle positioning automatically and you can use the child controls' Dock and Anchor properties to handle resizing automatically. You don't need an array or collection of your own because you already have the Controls collection of the TLP/FLP.
 
Awesome info!

I will do some research and look up some tutorials on using those controls but sounds like that is exactly what I need.
 
Back
Top Bottom