在表单上放置标签网格

时间:2014-09-20 14:57:35

标签: c# winforms label-control

我试图在我的winforms应用上放置标签网格。首先,我填充一个大小为(200 x 50)的标签对象列表,然后尝试放置它们,以便当x到达窗体的宽度(581)时,我将y递增50 + 1

这是我的代码:

private List<Label> _labels;
    private int xOffset = 10;
    private int yOffset = 10;

    public Form1()
    {
        InitializeComponent();

        _labels = new List<Label>();
        for(var i = 0; i <= 20; i++)
            _labels.Add(new Label() { Name = "lbl" + i, Height = 50, Width = 200, MinimumSize = new Size(200, 50), BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D, Text = "Label "+i});

        // 581, 517
        var x = 0;
        var y = 0;

        foreach (var lbl in _labels)
        {
            if (x >= 580)
            {
                x = 0;
                y = y + lbl.Height + 2;
                lbl.Location = new Point(x, y);
            }

            this.Controls.Add(lbl);
            x += x + lbl.Width;
        }
    }

它只将列表中的偶数标签放在新行上。我不确定我做错了什么。

我试图将所有标签放在网格设计中。当一行已满时,转到下一行并继续将列表中的标签放在新的&#34;行&#34;

5 个答案:

答案 0 :(得分:1)

有问题的部分在这里

x += x + lbl.Width; //+= x

将其更改为

x += lbl.Width;

获取

lbl.Location = new Point(x, y);

中的if语句

if (x >= 580)
  {
        x = 0;
        y = y + lbl.Height + 2;
        //lbl.Location = new Point(x, y);
  }
  lbl.Location = new Point(x, y);
  this.Controls.Add(lbl);
  x += lbl.Width;

答案 1 :(得分:1)

您需要将位置设置代码移出重置循环:

    foreach (var lbl in _labels)
    {
        if (x >= 580)
        {
            x = 0;
            y = y + lbl.Height + 2;
       }

         lbl.Location = new Point(x, y);
         this.Controls.Add(lbl);
        x +=  lbl.Width;
    }

答案 2 :(得分:0)

使用停靠FlowLayoutPanel

尝试此操作
public partial class Form1 : Form
{
    List<Label> labels;
    public Form1()
    {
        InitializeComponent();

        this.labels=new List<Label>();

        AddLabelsToFrom(20);
    }

    void AddLabelsToFrom(int count)
    {
        for (int i=0; i<count; i++)
        {
            var lbl=new Label() { Name="lbl"+i, Height=50, Width=200, MinimumSize=new Size(200, 50), BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D, Text="Label "+i };
            labels.Add(lbl);
            flowLayoutPanel1.Controls.Add(lbl);
        }
    }
}

SCR

答案 3 :(得分:0)

void SetGridLabel()
{
    for (int i = 0; ; i++)
    {
        for (int j = 0; ; j++)
        {
            Label L = new Label();
            L.TextAlign = ContentAlignment.MiddleCenter;
            L.AutoSize = false;
            L.Size = new Size(70, 70);
            L.Text = "Test_" + j + "_" + i;
            L.Location = new Point(j * L.Size.Width, i * L.Size.Height);

            if ((i + 1) * L.Size.Height > this.Size.Height)
                return;
            if ((j + 1) * L.Size.Width > this.Size.Width)
                break;
            this.Controls.Add(L);
        }

    }

}

enter image description here

答案 4 :(得分:0)

 private List<Label> _labels;
    public Form1()
    {
        InitializeComponent();

        _labels = new List<Label>();
        for (var i = 0; i <= 20; i++)
            _labels.Add(new Label()
            {
                Name = "lbl" + i, Height = 50,Width = 200,
                Size = MinimumSize = new Size(200, 50),
                Location = new Point(i * 200 % 600, 50 * (i * 200 / 600)),
                BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D,
                Text = "Label " + i
            });
        foreach (var lbl in _labels) this.Controls.Add(lbl);
    }