FlowLayoutPanel autosize

…衆ロ難τιáo~ 提交于 2019-12-10 18:19:23

问题


I have flow layout panel dock (Fill) in parent container. The Parent container DockStyle is set to Top. Also I set the FlowDirection property to LeftToRight for flow layout panel and the AutoSize property to True for both containers. The main function of flow layout panel is to keep dynamically added buttons.

The Code for creating Buttons

Button productButton = new Button();
productButton.AutoSize = true;
productButton.AutoEllipsis = false;
productButtonPanel.Controls.Add(productButton);

There is enough space to keep 10-12 buttons in one line and the FlowLayoutPanel is doing great. It keeps all new buttons in one line. The problem is that the FlowLayoutPanel resizes (vertically) every time I add new Button even when the extra space ("New button line") is not necessary.


回答1:


Maybe AutoSizeMode = AutoSizeMode.GrowAndShrink; will solve your problem. E.g.

        Form f = new Form();
        Panel parent = new Panel { Dock = DockStyle.Top, BackColor = Color.Blue, AutoSize = true  };

        FlowLayoutPanel p1 = new FlowLayoutPanel { FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight };
        p1.BackColor = Color.Red;
        p1.AutoSize = true;
        p1.AutoSizeMode = AutoSizeMode.GrowAndShrink;

        Button b1 = new Button { Text = "Button1", AutoSize = true, AutoEllipsis = false };
        p1.Controls.Add(b1);
        b1.Click += delegate {
            Button b2 = new Button { Text = "Button" + (p1.Controls.Count + 1), AutoSize = true, AutoEllipsis = false };
            p1.Controls.Add(b2);
        };
        parent.Controls.Add(p1);
        f.Controls.Add(parent);
        Application.Run(f);



回答2:


Finally i solved the problem. Flow Layout Panel was docked in Table Layout Panel ? It seems that Table Layout Panel is talking control over autosizing of child Flow Layout Panel. The workaround of this problem is:

Table Layout Panel (autosize = true)
    Panel (autosize = true, Dock=Fill)
        Table Layout Panel  (autosize = true, Dock=Fill)

More details: FlowLayoutPanel Height bug when using AutoSize



来源:https://stackoverflow.com/questions/30714980/flowlayoutpanel-autosize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!