简体   繁体   English

在C#中使用循环从列表创建对象

[英]create objects from list with loop in C#

Background: For the most part, I'm self taught in C#, so I apologize if this is some simple-minded problem. 背景:在大多数情况下,我是使用C#进行自学的,因此,如果这是一个简单的问题,我深表歉意。 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. 我在formMain中有一个列表,您可以在其中添加,编辑和删除单个标签,并且我有一个表单printPreview(one)用于从列表中选择的单个条目。

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. 您必须创建一个列表并添加它们,或者仅对于(i = 0-> N)添加它们。 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. 我认为TableLayoutPanel或FlowLayoutPanel将非常适合您的工作。 Drop a Table/FlowLayoutPanel onto your dialog, and then in your code programmatically make your groupboxes and add them to the table. 将Table / FlowLayoutPanel放到对话框中,然后在代码中以编程方式创建组框并将其添加到表中。 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. 如果您事先确定表格的大小,则不必担心查找位置,只需将它们一次添加一次,然后layoutpanel即可处理其余位置。


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. Winform控件的FlowlayoutPanel,TableLayoutPanel和Autosize属性是一些。 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. 创建新窗体,添加布局面板,开始创建所需标签大小的组框,将tablelayoutpanel的行/列大小设置为AutoSize(如果使用的话),然后开始粘贴。

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 http://msdn.microsoft.com/zh-CN/vstudio/Video/bb798032

Video on flowlayoutpanel: flowlayoutpanel上的视频:

http://msdn.microsoft.com/en-us/vstudio/Video/bb798028 http://msdn.microsoft.com/zh-CN/vstudio/Video/bb798028

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM