0

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?

4

1 に答える 1

1

あなたの言葉遣いは少し私を投げかけていますが、私はこれに対処しようとします. 私が道を外れていたら、私を許してください。

直接的で単純な答えに答えるために。グループボックスをフォームに追加:


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

リスト内の数と同じ数を作成するには、何らかの反復が必要になります。リストを作成して追加するか、単に for(i=0 -> N) 追加する必要があります。どのように表示するか (余白、パディング、サイズなど) に応じて、配置ごとに計算を行う必要があります。

さて、もう少し詳しく:

TableLayoutPanel または FlowLayoutPanel は、あなたがしていることに非常に適していると思います。Table/FlowLayoutPanel をダイアログにドロップし、コードでプログラムによってグループボックスを作成し、それらをテーブルに追加します。どちらもコントロールポジショニングの処理に優れています。事前にテーブルのサイズを設定する場合、場所を見つけることを心配する必要はありません。一度に 1 つずつ追加するだけで、レイアウトパネルが残りを処理します。


foreach(GroupBox groupbox in labelGroupBoxes)
{
    tableLayoutPanel.Controls.Add(groupbox);
}

これについては、いくつか確認していただきたい点があると思います。FlowlayoutPanel、TableLayoutPanel、および winform コントロールの Autosize プロパティはほんの一部です。新しいフォームを作成し、レイアウト パネルを追加し、必要なラベルのサイズのグループ ボックスの作成を開始し、tablelayoutpanel の行/列のサイズを AutoSize に設定し (使用する場合)、貼り付けを開始します。

これは、テーブル レイアウト パネルに関するかなり適切なビデオです。実際に探していたビデオが見つかりません...

http://msdn.microsoft.com/en-us/vstudio/Video/bb798032

flowlayoutpanel のビデオ:

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

于 2012-05-11T04:51:45.893 に答える