Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller wrapping in panel

Tags:

c#

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
like image 465
Mustafa E. Avatar asked Mar 12 '12 09:03

Mustafa E.


2 Answers

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);
        }
    }
like image 128
brgerner Avatar answered Nov 05 '22 01:11

brgerner


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
like image 1
Treb Avatar answered Nov 05 '22 03:11

Treb