使用winform 控件panel 创建一个界面,第一层有一个panel,第二层有多个panel此时第一层panel数量多第一层会出现纵向滚动条

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
         
            mainFlowLayoutPanel.AutoScroll = true;
            // 在第一层 Panel 中添加多个第二层 Panel
            for (int i = 0; i < 1000; i++)
            {
                Panel secondLayerPanel = new Panel();
                secondLayerPanel.BackColor = System.Drawing.Color.LightGray;
                secondLayerPanel.Size = new System.Drawing.Size(500, 300);
                secondLayerPanel.BackColor = GetRandomColor();

                // 在第二层 Panel 中添加控件或其他内容
                Label label = new Label();
                label.Text = $"Panel {i + 1}";
                label.Dock = DockStyle.Fill;
                label.TextAlign = ContentAlignment.MiddleCenter;

                secondLayerPanel.Controls.Add(label);

                // 将第二层 Panel 添加到第一层 Panel 中
                mainFlowLayoutPanel.Controls.Add(secondLayerPanel);
            }
           
        }

        private void LoadPanels()
        {
            // 第一个Panel,高度固定,带滚动条
            Panel topLevelPanel1 = CreatePanel("Top Level Panel 1", 500);
            topLevelPanel1.AutoScroll = true;  // 启用滚动条
            mainFlowLayoutPanel.Controls.Add(topLevelPanel1);

            // 第二个Panel,高度自适应
            Panel topLevelPanel2 = CreatePanel("Top Level Panel 2", 500);
            mainFlowLayoutPanel.Controls.Add(topLevelPanel2);

            // 加载第二层Panel,放在第一个Panel中,带滚动条
            Panel secondLevelPanel = CreatePanel("Second Level Panel", 500);
            secondLevelPanel.AutoScroll = true;  // 启用滚动条
            topLevelPanel1.Controls.Add(secondLevelPanel);

            // 加载第三层Panel,放在第二个Panel中
            Panel thirdLevelPanel = CreatePanel("Third Level Panel", 500);
            topLevelPanel2.Controls.Add(thirdLevelPanel);
        }

        private Panel CreatePanel(string name, int height)
        {
            Panel panel = new Panel
            {
                Name = name,
                Height = height,
                Dock = DockStyle.Top, // 设置为Top以确保占据空余空间
                BackColor = System.Drawing.Color.LightGray
            };

            // 添加一些子级控件或者Panel
            for (int i = 0; i < 100; i++)  // 增加子Panel数量以触发滚动条
            {
                Panel subPanel = new Panel
                {
                    Name = $"SubPanel{i + 1}",
                    Height = 40,
                    Dock = DockStyle.Top,
                    BackColor = GetRandomColor()
                };

                Label label = new Label
                {
                    Text = $"Label {i + 1}",
                    Location = new System.Drawing.Point(10, 10),
                    AutoSize = true
                };

                subPanel.Controls.Add(label);
                panel.Controls.Add(subPanel);
            }

            return panel;
        }


        static Color GetRandomColor()
        {
            Random random = new Random();

            // 生成随机的RGB值
            int red = random.Next(256);
            int green = random.Next(256);
            int blue = random.Next(256);

            // 使用Color.FromArgb创建颜色
            Color randomColor = Color.FromArgb(red, green, blue);

            return randomColor;
        }
    

    }
}

你可能感兴趣的:(c#,winform)