Controller wrapping in panel

I have a panel with buttons. my buttons are create dynamically. I want to have 4 rows with 4 buttons each. but I only get one row.

 foreach (CategoriesDataSet.CategoriesRow category in DataRepository.Categories.Categories)
        {
            if (!category.CategoryName.Equals("ROOT"))
            {
                SimpleButton button = new SimpleButton();
                button.Text = category.CategoryName;
                button.Tag = category.CategoryId;
                button.Size = new Size(82, 70);


                if (lastButton != null)
                    button.Left = lastButton.Right + 1;


                lastButton = button;
                button.Click += CategoryButtonClick;
                categoriesPanel.Controls.Add(button);
            }
        }

Desired result :

x x x x
X x x x 
x x x x

This is the answer followed by the comments of Treb's answer.
Use for loop and use modulo operator for Left property.

for (int i = 0; i < DataRepository.Categories.Categories.Count; i++)
{
    CategoriesDataSet.CategoriesRow category = DataRepository.Categories.Categories[i];

        if (!category.CategoryName.Equals("ROOT"))
        {
            SimpleButton button = new SimpleButton();
            button.Text = category.CategoryName;
            button.Tag = category.CategoryId;
            button.Size = new Size(82, 70);

            button.Left = i%4*82;
            button.Top = i*70;

            button.Click += CategoryButtonClick;
            categoriesPanel.Controls.Add(button);
        }
    }

You are only moving buttons to the right (horizontally) by adjusting the Left property:

button.Left = lastButton.Right + 1;

If you want to move them down, you need to adjust the vertical position instead:

button.Top = lastButton.Bottom + 1;

Since that way you leave the horizontal position unchanged, the new button should appear directly below the last one.

EDIT: In answer to your comment, which result do you want? Is it A)

X X X X

or B)

X
X  
X  
X  

or is it C)

X
 X  
  X  
   X  

Or with buttons by lines, do you mean something like D)

X | X | X | X

or E)

X - X - X - X
链接地址: http://www.djcxy.com/p/10564.html

上一篇: Ember.js和handlebars每个助手,与子视图

下一篇: 控制器包裹在面板中