How to hide dynamically created controls in scrollable panel

I have a problem, because in my code I am dynamically creating new buttons, and after that, the window looks in that way:

This is code that I used for that:

private void DrawButtons()
{
    for (int i = 0; i < 90; i++)
    {

        Button button = new Button();
        button.Location = new Point(15 + 40 * i, 10);
        button.Size = new Size(35, 30);
        button.Parent = panel4;

        button.Tag = i;
        Controls.Add(button);

        button.BringToFront();

    }
}

I want to have scrollable panel, like there, where I created buttons manually:

What I must do to have this effect with programically created elements?


You can use the AutoScroll property. For Panel :

panel4.AutoScroll = true;

But you should also set this property:

button.Anchor = AnchorStyles.Left;

And also add the button to your Panel :

panel4.Controls.Add(button);

So this should be what you want:

 private void DrawButtons()
 {
     for (int i = 0; i < 90; i++)
     {
         ...
         button.Anchor = AnchorStyles.Left;
         ...
         panel4.Controls.Add(button);//Add this also
         ...
     }
     panel4.AutoScroll = true;
 }

The result:

链接地址: http://www.djcxy.com/p/61576.html

上一篇: 控件未在动态生成的面板中显示

下一篇: 如何在可滚动面板中隐藏动态创建的控件