用FlowLayoutPanel做侧边栏

    /// 
    /// 设置内容面板
    /// 
    /// 要设置的内容面板
    /// 要添加的按钮Text集合
    /// 要在Button中执行的方法(方法可以写动态加载的菜单)
    public static void SetFlowLayoutPanel(FlowLayoutPanel flp,List btns,Action act)
    {
        Panel pl = new Panel();
        pl.BackColor = Color.Black;
        for (int i = 0; i < btns.Count; i++)
        {
            Button btn = new Button();
            btn.Text = btns[i];
            btn.Click += (a, b) =>
            {
                btn.BackColor = Color.YellowGreen;
                flp.Controls.SetChildIndex(btn, 0);
                flp.Controls.SetChildIndex(pl, 1);
                act();
            };
            btn.LostFocus += (a, b) =>
            {
                btn.BackColor = Color.FromKnownColor(KnownColor.Control);
            };
            btn.Width = flp.Width - 2;
            flp.Controls.Add(btn);
        }
        flp.Controls.Add(pl);
    }

你可能感兴趣的:(用FlowLayoutPanel做侧边栏)