通过循环创建复选框时,仅显示第一个复选框

时间:2017-06-30 04:43:56

标签: c# for-loop checkbox

我有一个用户定义的"标签列表"存储在名为warehouse.tags的字符串列表中。使用基本的for循环,我为每个标签创建复选框,并将它们添加到Windows窗体中的面板。

问题是只显示第一个复选框。我多次遍历代码,位置坐标和其他属性似乎正在工作。问题似乎不是复选框位置在面板边界之外。

   for(int i = 0; i < warehouse.tags.Count; i++) //adds check boxes for each tag
        {
            CheckBox tagNameLabel = new CheckBox();
            tagNameLabel.Text = warehouse.tags.ElementAt(i);
            Point tagLabelPoint = new Point();
            tagLabelPoint.X = xAdjuster; // xadjuster = 25 in this case 
            tagLabelPoint.Y = (5 + yAdjuster) * (warehouse.categories.Count + 1); //yadjuster = 25 as well
            tagNameLabel.Location = tagLabelPoint;
            this.filterOptionsPanel.Controls.Add(tagNameLabel);

        }

对任何可能出错的想法持开放态度 - 谢谢。

1 个答案:

答案 0 :(得分:6)

问题是您的所有复选框都将设置为相同的位置。

tagLabelPoint.X = xAdjuster; // xadjuster = 25 in this case 
tagLabelPoint.Y = (5 + yAdjuster) * (warehouse.categories.Count + 1);

由于您未在此公式中使用i,因此每个复选框都将设置为相同的位置。请注意,yAdjusterwarehouse.categories.Count永远不会在循环过程中发生变化。