123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- 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<int> pages = new BindingList<int>();
- /// <summary>
- /// 页面数量
- /// </summary>
- [Description("当前每次分页个数")]
- public int PageSize
- {
- get { return this._pageSzie; }
- set
- {
- if (this._pageSzie != value)
- {
- this.SetPageSize(value);
- }
- }
- }
- private int _pageIndex = 0;
- /// <summary>
- /// 当前页面
- /// </summary>
- [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<string> _PageSizeSelectList = new List<string>() { "10", "20", "30", "40", "50", "100" };
- [Description("页面个数选择列表")]
- public List<string> 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 分页控件
- /// <summary>
- /// 每页标签
- /// </summary>
- KryptonLabel lbEveryPage;
- /// <summary>
- /// 每页记录数列表控件
- /// </summary>
- KryptonComboBox cmbEveryPageRecordNum;
- /// <summary>
- /// 记录标签
- /// </summary>
- KryptonLabel lbRecordsNum;
- /// <summary>
- /// 分割线
- /// </summary>
- KryptonSeparator separator;
- /// <summary>
- /// 首页按钮
- /// </summary>
- KryptonButton BtnFirst;
- /// <summary>
- /// 上一页按钮
- /// </summary>
- KryptonButton BtnBack;
- /// <summary>
- /// 当前页标签
- /// </summary>
- KryptonLabel lbCurrentPage;
- /// <summary>
- /// 总页数标签
- /// </summary>
- KryptonLabel lbTotalPage;
- /// <summary>
- /// 数据记录总数标签
- /// </summary>
- KryptonLabel lbTotalDataRecords;
- /// <summary>
- /// 下一页按钮
- /// </summary>
- KryptonButton BtnNext;
- /// <summary>
- /// 尾页按钮
- /// </summary>
- KryptonButton BtnLast;
- /// <summary>
- /// 分割线
- /// </summary>
- KryptonSeparator separator1;
- /// <summary>
- /// 页码标签控件
- /// </summary>
- KryptonLabel lbPageNum;
- /// <summary>
- /// 页码列表控件
- /// </summary>
- KryptonComboBox cmbPageNumEnum;
- /// <summary>
- /// 分割线
- /// </summary>
- KryptonSeparator separator2;
- /// <summary>
- /// 跳转页码按钮
- /// </summary>
- 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;
- }
- }
- }
|