【问题标题】:Place a grid of labels on a form在表单上放置标签网格
【发布时间】:2014-09-20 14:57:35
【问题描述】:

我正在尝试在我的 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;
        }
    }

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

我正在尝试将所有标签放置在类似设计的网格中。当一行已满时,转到下一行并继续将列表中的标签放在新“行”上

【问题讨论】:

  • 问题出在哪里?
  • 对不起,我忘了添加那部分。请参阅我的编辑。我正在尝试将它们全部绘制在网格中的表单上(所以行和列)
  • 为什么不使用 FlowLayoutPanel 并将方向设置为水平??
  • 当你指的是地点时,请不要说绘制。
  • 为什么不使用TableLayoutPanelFlowLayoutPanel

标签: c# winforms label-control


【解决方案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;
    }

【讨论】:

  • 是的,这是一场激烈的比赛;-)
  • 哇,我没有意识到这一点。这正是我一直在寻找的!我不想使用 FlowLayoutPanel 或 TableLayoutPanel
【解决方案2】:

有问题的部分在这里

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;

【讨论】:

  • 现在只画0、3、6、9、12、15、18
【解决方案3】:

使用停靠的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);
        }
    }
}

【讨论】:

    【解决方案4】:
    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);
            }
    
        }
    
    }
    

    【讨论】:

      【解决方案5】:
       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);
          }
      

      【讨论】:

        猜你喜欢
        • 2014-06-14
        • 2013-04-05
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 1970-01-01
        • 2018-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多