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 NXWMS.Client.Model.CoreModels; using ComponentFactory.Krypton.Toolkit; using System.Threading; using System.Threading.Tasks; namespace NXWMS.Client.FrmCustom { public partial class PageToolbar : UserControl { private int _pageSzie = 30; private BindingList pages = new BindingList(); /// /// 页面数量 /// [Description("当前每次分页个数")] public int PageSize { get { return this._pageSzie; } set { if (this._pageSzie != value) { this.SetPageSize(value); } } } private int _pageIndex = 0; /// /// 当前页面 /// [Description("当前页码")] public int PageIndex { get { if (this._PageTotalIndex == 0) { return 1; } return this._pageIndex; } //set //{ // this.SetPageIndex(value); //} } public int _PageTotalIndex; [Description("最新总页码")] public int PageTotalIndex { get { return this._PageTotalIndex; } } private int _TotalCount = 0; [Description("数据总个数")] public int DataCount { get { return this._TotalCount; } set { if (this._TotalCount != value) { this.SetTotalCount(value); } } } private List _PageSizeSelectList = new List() { "10", "20", "30", "40", "50", "100" }; [Description("页面个数选择列表")] public List PageSizeSelectList { get { return this._PageSizeSelectList; } set { this._PageSizeSelectList = value; } } private int _PageIndexSelectDefault = 1; [Description("默认选择页码")] public int PageIndexSelectDefault { get { return this._PageIndexSelectDefault; } set { this._PageIndexSelectDefault = value; } } [Description("页面分页匹配(2个占位符)")] public string PageDefault { get; set; } = "{0}/{1}"; [Description("页面默认文本(1个占位符)")] public string PageInfoDefault { get; set; } = "共 {0} 条记录,每页"; [Description("加载消息文本(2个占位符,1:当前第几页数据,2:已多少记录分页)")] public string LoadMessage { get; set; } //@"正在加载第 {0} 页数据,以 {1} 条记录进行分页。"; public delegate void PageChange(int PageIndex, int PageSzie); public void DoOnPageChange(int PageIndex, int PageSzie) { if (PageIndex == 0) return; //if (this.inLoad) return; if (OnPageChange != null) { OnPageChange(PageIndex, PageSzie); } } public event PageChange OnPageChange; //TODO 这里需要修改到使用方法 public delegate OperateResultInfo ExecActionOperation(int pageIndex, int pageSize); public delegate string ExecActionMessgae(int pageIndex, int pageSize); public ExecActionOperation _execActionOperation { get; set; } public ExecActionMessgae _execActionMessgae { get; set; } public OperateResultInfo _LastOperation { get; set; } private void ControlEnable() { if (this._PageTotalIndex == 0) { BtnBack.Enabled = false; BtnNext.Enabled = false; BtnFirst.Enabled = false; BtnLast.Enabled = false; return; } if (this._pageIndex == 1) { BtnBack.Enabled = false; BtnFirst.Enabled = false; } else { BtnBack.Enabled = true; BtnFirst.Enabled = true; } if (this._pageIndex == this._PageTotalIndex) { BtnNext.Enabled = false; BtnLast.Enabled = false; } else { BtnNext.Enabled = true; BtnLast.Enabled = true; } } #region 分页控件 /// /// 每页标签 /// KryptonLabel lbEveryPage; /// /// 每页记录数列表控件 /// KryptonComboBox cmbEveryPageRecordNum; /// /// 记录标签 /// KryptonLabel lbRecordsNum; /// /// 分割线 /// KryptonSeparator separator; /// /// 首页按钮 /// KryptonButton BtnFirst; /// /// 上一页按钮 /// KryptonButton BtnBack; /// /// 当前页标签 /// KryptonLabel lbCurrentPage; /// /// 总页数标签 /// KryptonLabel lbTotalPage; /// /// 数据记录总数标签 /// KryptonLabel lbTotalDataRecords; /// /// 下一页按钮 /// KryptonButton BtnNext; /// /// 尾页按钮 /// KryptonButton BtnLast; /// /// 分割线 /// KryptonSeparator separator1; /// /// 页码标签控件 /// KryptonLabel lbPageNum; /// /// 页码列表控件 /// KryptonComboBox cmbPageNumEnum; /// /// 分割线 /// KryptonSeparator separator2; /// /// 跳转页码按钮 /// KryptonButton btGotoPage; #endregion private void InitPageControl() { #region 分页控件 lbEveryPage = new KryptonLabel(); lbEveryPage.Text = "每页"; lbEveryPage.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(lbEveryPage); this.ts_PageControl.Items.Insert(0, new ToolStripControlHost(lbEveryPage)); cmbEveryPageRecordNum = new KryptonComboBox(); cmbEveryPageRecordNum.Height = 23; cmbEveryPageRecordNum.Width = 40; cmbEveryPageRecordNum.MinimumSize = new Size(40, 23); cmbEveryPageRecordNum.PaletteMode = PaletteMode.Office2007Silver; // cmbEveryPageRecordNum.Items.AddRange(new object[] { 1, 5, 10, 15, 25, 50, 100 }); cmbEveryPageRecordNum.DropDownStyle = ComboBoxStyle.DropDownList; //cmbEveryPageRecordNum.SelectedIndex = 0; Controls.Add(cmbEveryPageRecordNum); this.ts_PageControl.Items.Insert(1, new ToolStripControlHost(cmbEveryPageRecordNum)); lbRecordsNum = new KryptonLabel(); lbRecordsNum.Text = "条记录"; lbRecordsNum.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(lbRecordsNum); this.ts_PageControl.Items.Insert(2, new ToolStripControlHost(lbRecordsNum)); separator = new KryptonSeparator(); separator.Height = 25; separator.Width = 5; separator.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(separator); this.ts_PageControl.Items.Insert(3, new ToolStripControlHost(separator)); BtnFirst = new KryptonButton(); BtnFirst.Text = "首页"; BtnFirst.Height = 25; BtnFirst.Width = 25; BtnFirst.AutoSize = true; BtnFirst.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(BtnFirst); this.ts_PageControl.Items.Insert(4, new ToolStripControlHost(BtnFirst)); BtnBack = new KryptonButton(); BtnBack.Text = "上一页"; BtnBack.Height = 25; BtnBack.Width = 25; BtnBack.AutoSize = true; BtnBack.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(BtnBack); this.ts_PageControl.Items.Insert(5, new ToolStripControlHost(BtnBack)); lbCurrentPage = new KryptonLabel(); lbCurrentPage.Tag = 1; lbCurrentPage.Text = "第0页"; lbCurrentPage.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(lbCurrentPage); this.ts_PageControl.Items.Insert(6, new ToolStripControlHost(lbCurrentPage)); lbTotalPage = new KryptonLabel(); lbTotalPage.Tag = 10; lbTotalPage.Text = "共0页"; lbTotalPage.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(lbTotalPage); this.ts_PageControl.Items.Insert(7, new ToolStripControlHost(lbTotalPage)); lbTotalDataRecords = new KryptonLabel(); lbTotalDataRecords.Tag = 10; lbTotalDataRecords.Text = "总计:0条数据记录"; lbTotalDataRecords.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(lbTotalDataRecords); this.ts_PageControl.Items.Insert(8, new ToolStripControlHost(lbTotalDataRecords)); BtnNext = new KryptonButton(); BtnNext.Text = "下一页"; BtnNext.Height = 25; BtnNext.Width = 25; BtnNext.AutoSize = true; BtnNext.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(BtnNext); this.ts_PageControl.Items.Insert(9, new ToolStripControlHost(BtnNext)); BtnLast = new KryptonButton(); BtnLast.Text = "尾页"; BtnLast.Height = 25; BtnLast.Width = 25; BtnLast.AutoSize = true; BtnLast.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(BtnLast); this.ts_PageControl.Items.Insert(10, new ToolStripControlHost(BtnLast)); separator1 = new KryptonSeparator(); separator1.Height = 25; separator1.Width = 5; separator1.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(separator1); this.ts_PageControl.Items.Insert(11, new ToolStripControlHost(separator1)); lbPageNum = new KryptonLabel(); lbPageNum.Text = "页码:"; Controls.Add(lbPageNum); this.ts_PageControl.Items.Insert(12, new ToolStripControlHost(lbPageNum)); cmbPageNumEnum = new KryptonComboBox(); cmbPageNumEnum.Height = 23; cmbPageNumEnum.Width = 70; cmbPageNumEnum.MinimumSize = new Size(70, 23); cmbPageNumEnum.PaletteMode = PaletteMode.Office2007Silver; cmbPageNumEnum.DataSource = this.pages; Controls.Add(cmbPageNumEnum); this.ts_PageControl.Items.Insert(13, new ToolStripControlHost(cmbPageNumEnum)); separator2 = new KryptonSeparator(); separator2.Height = 25; separator2.Width = 5; separator2.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(separator2); this.ts_PageControl.Items.Insert(14, new ToolStripControlHost(separator2)); btGotoPage = new KryptonButton(); btGotoPage.Text = "跳转"; btGotoPage.Height = 25; btGotoPage.Width = 25; btGotoPage.AutoSize = true; btGotoPage.PaletteMode = PaletteMode.Office2007Silver; Controls.Add(btGotoPage); this.ts_PageControl.Items.Insert(15, new ToolStripControlHost(btGotoPage)); #endregion #region 注册按钮事件 cmbEveryPageRecordNum.SelectionChangeCommitted += CmbEveryPageRecordNum_SelectionChangeCommitted; ; BtnFirst.Click += this.kryptonBtnFirst_Click; BtnBack.Click += this.kryptonBtnBack_Click; BtnNext.Click += this.kryptonBtnNext_Click; BtnLast.Click += this.kryptonBtnLast_Click; btGotoPage.Click += BtGotoPage_Click; #endregion } private void BtGotoPage_Click(object sender, EventArgs e) { var _pageindex = Convert.ToInt32(this.cmbPageNumEnum.SelectedValue); this.SetPageIndex(_pageindex); } private void CmbEveryPageRecordNum_SelectionChangeCommitted(object sender, EventArgs e) { PageSize = Convert.ToInt32(cmbEveryPageRecordNum.Text); } public PageToolbar() { InitializeComponent(); InitPageControl(); this.ControlEnable(); cmbEveryPageRecordNum.DataSource = this.PageSizeSelectList; cmbPageNumEnum.Text = PageIndexSelectDefault.ToString(); this.pages.ListChanged += Pages_ListChanged; this.SetPageIndex(1); } private void Pages_ListChanged(object sender, ListChangedEventArgs e) { } private void SetPageSize(int pageSize, bool CountChange = false) { if (pageSize <= 0) { return; } var _totlaPages = (int)Math.Ceiling((double)this.DataCount / pageSize); this._PageTotalIndex = _totlaPages; this.lbTotalPage.Text = $"共{_totlaPages}页"; this.cmbPageNumEnum.SelectedIndexChanged -= this.CmbEveryPageRecordNum_SelectionChangeCommitted; this.cmbPageNumEnum.DataSource = null; lock (this.pages) { this.pages.Clear(); for (int n = 1; n <= _PageTotalIndex; n++) { this.pages.Add(n); } } this.cmbPageNumEnum.DataSource = pages; this.cmbPageNumEnum.SelectedIndexChanged += this.CmbEveryPageRecordNum_SelectionChangeCommitted; this._pageSzie = pageSize; cmbEveryPageRecordNum.Text = PageSize.ToString(); if (_totlaPages == 0) { this._pageIndex = 0; } else { if (this._pageIndex == 0) { this._pageIndex = 1; } } this.SetPageIndex(Math.Min(_totlaPages, this._pageIndex), CountChange); //if (_pageIndex == 1 || _totlaPages != 0) //{ // this.lbCurrentPage.Text = $"第{ this._pageIndex}页"; //} //else if (_pageIndex == 1 || _totlaPages == 0) //{ // this.lbCurrentPage.Text = $"第0页"; //} this.ControlEnable(); } private void SetTotalCount(int totalCount) { this._TotalCount = totalCount; this.lbTotalDataRecords.Text = $"总计:{this._TotalCount}条数据记录"; this.SetPageSize(this.PageSize, true); } private bool isIndex = false; public void SetPageIndex(int pageIndex, bool CountChange = false) { if (pageIndex < 0 || pageIndex > this._PageTotalIndex) { return; } if (isIndex) return; isIndex = true; this._pageIndex = pageIndex; this.lbCurrentPage.Text = $"第{ this._pageIndex}页"; this._PageTotalIndex = (int)Math.Ceiling((double)DataCount / PageSize); cmbPageNumEnum.Text = PageIndex.ToString(); if (!CountChange) this.DoOnPageChange(this.PageIndex, this._pageSzie); isIndex = false; this.ControlEnable(); } public void kryptonBtnFirst_Click(object sender, EventArgs e) { this.SetPageIndex(1); } public void kryptonBtnBack_Click(object sender, EventArgs e) { this.SetPageIndex(this._pageIndex - 1); } private static object obj = new object(); public void kryptonBtnNext_Click(object sender, EventArgs e) { this.SetPageIndex(this._pageIndex + 1); } public void kryptonBtnLast_Click(object sender, EventArgs e) { this.SetPageIndex(this._PageTotalIndex); // PageIndex = PageTotalIndex; } } }