简体   繁体   English

用标签集填充控件

[英]Populate control with set of labels

I'm writing a program that get data from the registry and then adds groupboxes to a tabcontrol, and in the groupbox I'd like to create as many label controls (to display the registry info) as many data (key-value pairs) I got. 我正在编写一个程序,该程序从注册表中获取数据,然后将组框添加到tabcontrol中,并且我希望在组框中创建与数据(键值对)一样多的标签控件(以显示注册表信息)我有。 To do this I made this function: 为此,我做了以下功能:

private void AddAllControl()
{
    GroupBox TestGroupBox = new GroupBox();
    TestGroupBox.AutoSize = true;
    TestGroupBox.Text = "valami";
    TestGroupBox.Height = 500;
    for (int i = 0; i < 21; i++)
    {
        Label TempLabel = new Label();
        TempLabel.Text = i.ToString();
        TempLabel.Location = new System.Drawing.Point(20 + i, 30);
        TempLabel.Show();
        TempLabel.Visible = true;
        TempLabel.Enabled = true;
        TestGroupBox.Controls.Add(TempLabel);               
    }
    tabPage_SandBox.Controls.Add(TestGroupBox);
}

This function is processed when a button been pressed. 当按下按钮时,将处理此功能。 After that The groupbox appear correctly, but only 1 label the first (with text = 0) appear instead of 21 label. 之后,该分组框将正确显示,但第一个(文本= 0)仅显示1个标签,而不是21个标签。 When I stop to debug the program I see all the labels are exists and all the property are correct, however they do not appear. 当我停止调试程序时,我看到所有标签都存在并且所有属性都正确,但是它们没有出现。 There must be something that I didn't noticed. 肯定有些东西我没注意到。 And now my question? 现在我的问题呢? What did I wrong? 我怎么了 As you can see I tried both visible and enabled property but neither of bring me solution. 如您所见,我尝试了visibleenabled属性,但都没有带来解决方案。

You have to set 你必须设置

  TempLabel.AutoSize = true;

And you have to modify the location a little bit like 而且您必须像这样修改位置

 TempLabel.Location = new System.Drawing.Point(20 + 10 * i, 30);

or I think you want to have the labels one below the other so you have to set the location like 或者我认为您希望将标签一个接一个地放置,因此您必须将位置设置为

 TempLabel.Location = new System.Drawing.Point(20, 20+20 * i);

If your Labels are a constant size then 如果您的标签尺寸不变,则

TestGroupBox.Controls.Add(new Label()
{
    Text = i.ToString(),
    Location = new Point(20 + (i*20), 30),
    Size = new Size(20, 20)
});

Would do the trick 会做到的

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

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