资源部分在QQ群:616945527
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; //CreateByZGJ 1981633 namespace BaseUserControl {
public partial class UCAlphaCollapseBar : UserControl { public enum PressButton { COLLAPSE_BUTTON=1, ALL_BUTTON, ALPHA_BUTTON } /// <summary> /// 代理事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="pressButton">点击的按钮</param> /// <param name="data">按钮附加信息</param> public delegate void PressBarButtonHandler(object sender, EventArgs e,PressButton pressButton,String data); //添加事件代理 public event PressBarButtonHandler BarButtonClick; public Boolean bCollapse = false; private int orgParentWidth = 0; public UCAlphaCollapseBar() { InitializeComponent(); buttonALL.Click+= new EventHandler(ControlClickEvent); buttonCollapse.Click += new EventHandler(ControlClickEvent); } /// <summary> /// 添加按钮 /// </summary> /// <param name="dataList"></param> public void AddButton(List<string> dataList,bool bSort=false) { ClearButtons(); if (bSort) dataList.Sort(); for (int i = 0; i < dataList.Count; i++) { Button button = new Button(); button.Margin = Padding.Empty; button.Text = dataList[i]; button.Height = 30; button.FlatStyle = FlatStyle.Flat; button.FlatAppearance.BorderSize = 0; button.TextAlign = ContentAlignment.MiddleCenter; button.Dock = DockStyle.Top; panelMain.Controls.Add(button); button.BringToFront(); button.Tag = "alphabutton"; button.Click += new EventHandler(ControlClickEvent); } } /// <summary> /// 重置所有按钮颜色 /// </summary> private void ResetAllButtonsColor() { buttonALL.ForeColor = Color.Black; //不删除前两个按钮 for (int i = panelMain.Controls.Count - 1; i >= 0; i--) { panelMain.Controls[i].ForeColor=Color.Black; } } /// <summary> /// 清空按钮 /// </summary> public void ClearButtons() { //不删除前两个按钮 for (int i = panelMain.Controls.Count-1; i >=0 ; i--) { panelMain.Controls[i].Dispose(); } } protected void ControlClickEvent(object sender, EventArgs e) { Button button = (Button)sender; PressButton pressButton; //点了全部按钮 if (buttonALL==button) { ResetAllButtonsColor(); buttonALL.ForeColor = Color.DarkGreen; pressButton = PressButton.ALL_BUTTON; BarButtonClick(sender, e, pressButton, ""); } else if(buttonCollapse==button) { if(buttonCollapse.Text=="<<") { for (int i = 0; i < this.Parent.Controls.Count; i++) { if(this.Parent.Controls[i]!=this) { this.Parent.Controls[i].Visible = false; } } orgParentWidth = this.Parent.Width; this.Parent.Width = this.Width; buttonCollapse.Text = ">>"; bCollapse = true; } else { for (int i = 0; i < this.Parent.Controls.Count; i++) { if (this.Parent.Controls[i] != this) { this.Parent.Controls[i].Visible = true; } } this.Parent.Width = orgParentWidth; buttonCollapse.Text = "<<"; bCollapse = false; } pressButton = PressButton.COLLAPSE_BUTTON; BarButtonClick(sender, e, pressButton, bCollapse.ToString()); } else { if(button.Tag.ToString()== "alphabutton") { ResetAllButtonsColor(); button.ForeColor = Color.DarkGreen; } pressButton = PressButton.ALPHA_BUTTON; BarButtonClick(sender, e, pressButton, button.Text); } } } }