public MainForm() { InitializeComponent(); button1.Click += new EventHandler(btn_Click); button2.Click += new EventHandler(btn_Click); button3.Click += new EventHandler(btn_Click); button4.Click += new EventHandler(btn_Click); button5.Click += new EventHandler(btn_Click); leftMenu(); } private void leftMenu() { button5.Tag = button4.Tag = button3.Tag = button1.Tag = button2.Tag = 1; panel5.Visible = panel4.Visible = panel3.Visible = panel1.Visible = panel2.Visible = false; } //折叠按钮 private void btn_Click(object sender, EventArgs e) { Button btn = sender as Button; foreach (Control x in splitContainer1.Panel1.Controls) { if (x.Name == "panel" + btn.Name[btn.Name.Length - 1]) { if ((int)btn.Tag == 2) { btn.Tag = 1; x.Visible = false; return; } else { btn.Tag = 2; x.Visible = true; } break; } } int tmpUp = Convert.ToInt32(btn.Name[btn.Name.Length - 1].ToString()) - 1; int tmpDown = Convert.ToInt32(btn.Name[btn.Name.Length - 1].ToString()) + 1; foreach (Control x in splitContainer1.Panel1.Controls) { if (tmpUp != 1) { if (x.Name == "panel" + tmpUp) x.Visible = false; if (x.Name == "button" + tmpUp) x.Tag = 1; } if (x.Name == "panel" + tmpDown) x.Visible = false; if (x.Name == "button" + tmpDown) x.Tag = 1; } }
左侧菜单,2个控件一个按妞和一个面板,通过Name属性,使按钮和面板联系在一起,