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; using JwKdsV.Core; using System.Threading; namespace JwKdsV.Component { public partial class PagerControl : UserControl { public PagerControl() { InitializeComponent(); } private int _totalCount = 0; /// /// 总条数 /// [Category("杂项"), Description("总条数")] public int TotalCount { get { return _totalCount; } set { if(_totalCount == value) { return; } _totalCount = value; CalculatePageCount(); } } private int _pageSize = 5; /// /// 每页展示数量 /// [Category("杂项"), Description("每页展示数量")] public int PageSize { get { return _pageSize; } set { _pageSize = value; CalculatePageCount(); } } /// /// 总页数 /// [Category("杂项"), Description("总页数")] public int PageCount { get; set; } private int _currentPage; /// /// 当前页数 /// [Category("杂项"), Description("当前页数")] public int CurrentPage { get { return _currentPage; } set { if(_currentPage == value) { return; } _currentPage = value; RefreshUI(); } } private void CalculatePageCount() { if(PageSize == 0) { //排除错误设置 return; } if(TotalCount % PageSize == 0) { PageCount = TotalCount / PageSize; } else { PageCount = TotalCount / PageSize + 1; } //默认显示第一页 this.CurrentPage = 1; RefreshUI(); } /// /// 更新UI /// private void RefreshUI() { this.upButton.Enabled = true; this.downButton.Enabled = true; if(CurrentPage <= 1) { this.upButton.Enabled = false; } if(CurrentPage >= PageCount) { this.downButton.Enabled = false; } this.pageLabel.Text = string.Format("{0}/{1}", CurrentPage, PageCount); } private Font _font = Constant.NORMAL_FONT; [Category("杂项"), Description("字体")] public Font PanelFont { get { return this._font; } set { this._font = value; this.upButton.Font = this._font; this.pageLabel.Font = this._font; this.downButton.Font = this._font; } } private void OnPageChangeClick(object sender, EventArgs e) { if(sender is Button) { var obj = sender as Button; var target = obj.Tag as string; if(target == "up") { //上一页 this.CurrentPage--; } else { //下一页 this.CurrentPage++; } OnPageChange(this); } } public event Action PageChangeWithObject; private void OnPageChange(PagerControl sender) { Action temp = Interlocked.CompareExchange(ref PageChangeWithObject, null, null); if (temp != null) { temp(sender); } } } }