Background: For the most part, I'm self taught in C#, so I apologize if this is some simple-minded problem. I am creating something to the effect of a mailing list (each object has name, address, contact info, etc) and will be printing out in labels (there will be two colums and four rows per printed page). I have the list in formMain where you can add, edit and delete individual labels and I have a form printPreview(one) for individual entries selected from the list.
The problem: I'm trying to create a print preview form for the whole list; generating a group box (containing a text box and picture box) for each object from the list - that way I'll have exactly the number of labels as objects - then fill each box with the content respective to each object on the list. Each group box, text box and picture box are specific sizes and will be spaced so there's room between each label. So here's the pseudo code I'm trying to make happen;
//box[num] contains
//text box at location(6,19)
//picture box at location(222,19)
int locX = 0;
int locY = 0;
listObj = list.first;
for (int i = 0; i < list.count; i++)
{
//create box[i] at location (locX, locY);
box[i].textbox.text = listObj.text;
box[i].picturebox.image = Image.FromFile(listObj.photoLocation);
if(i%2)
{
locX+=400;
}
else
{
locY+=248;
locX=0;
}
listObj = listObj.next;
}
Now, I know there's a lot of holes in there, but I just need the basic: how do I get my program to create new group boxes in a form equal to the number of objects in my list?
Your wording is throwing me a little, but I'm going to try and address this. Forgive me if I'm way off.
To answer the direct, simple answer; adding a groupbox to a form:
GroupBox groupbox1 = new System.Windows.Forms.GroupBox();
groupbox1.Location = new System.Drawing.Point(x, y);
form1.Controls.Add(groupbox1);
Textbox textbox = new System.Windows.Forms.Textbox();
textbox.Location = new System.Drawing.Point(x2, y2);
groupbox1.Controls.Add(textbox);
// same for picturebox, where x/x2 and y/y2 are your calculated
// placements of the controls
Making an equal number of them as the number in your list will require some sort of iteration; you'll have to either make a List and add them, or just for(i=0 -> N) add them. You'll have to work out the math on the placement though for each one, depending on how you want it to look (margins, padding, size, etc.)
Now, in a little more detail:
I think a TableLayoutPanel or FlowLayoutPanel would be a very good fit for what you're doing. Drop a Table/FlowLayoutPanel onto your dialog, and then in your code programmatically make your groupboxes and add them to the table. Both are excellent at handling control positioning for you. If you size the table before hand, you shouldn't need to worry about finding the locations, you can just add them in one at a time and the layoutpanel handle the rest.
foreach(GroupBox groupbox in labelGroupBoxes)
{
tableLayoutPanel.Controls.Add(groupbox);
}
There are a couple of things you'll want to look at for this, I think. The FlowlayoutPanel, TableLayoutPanel and the Autosize property of winform controls are a few. Create your new Form, add a layout panel, start creating groupboxes the size of the labels you want, set the row/column sizes of the tablelayoutpanel to AutoSize if you use that one, and start sticking them in.
Here's a reasonably good video on table layout panel, I can't find the one I was actually looking for...
http://msdn.microsoft.com/en-us/vstudio/Video/bb798032
Video on flowlayoutpanel: