PageToolbar.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using NXWMS.Client.Model.CoreModels;
  10. using ComponentFactory.Krypton.Toolkit;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace NXWMS.Client.FrmCustom
  14. {
  15. public partial class PageToolbar : UserControl
  16. {
  17. private int _pageSzie = 30;
  18. private BindingList<int> pages = new BindingList<int>();
  19. /// <summary>
  20. /// 页面数量
  21. /// </summary>
  22. [Description("当前每次分页个数")]
  23. public int PageSize
  24. {
  25. get { return this._pageSzie; }
  26. set
  27. {
  28. if (this._pageSzie != value)
  29. {
  30. this.SetPageSize(value);
  31. }
  32. }
  33. }
  34. private int _pageIndex = 0;
  35. /// <summary>
  36. /// 当前页面
  37. /// </summary>
  38. [Description("当前页码")]
  39. public int PageIndex
  40. {
  41. get
  42. {
  43. if (this._PageTotalIndex == 0)
  44. {
  45. return 1;
  46. }
  47. return this._pageIndex;
  48. }
  49. //set
  50. //{
  51. // this.SetPageIndex(value);
  52. //}
  53. }
  54. public int _PageTotalIndex;
  55. [Description("最新总页码")]
  56. public int PageTotalIndex
  57. {
  58. get { return this._PageTotalIndex; }
  59. }
  60. private int _TotalCount = 0;
  61. [Description("数据总个数")]
  62. public int DataCount
  63. {
  64. get
  65. {
  66. return this._TotalCount;
  67. }
  68. set
  69. {
  70. if (this._TotalCount != value)
  71. {
  72. this.SetTotalCount(value);
  73. }
  74. }
  75. }
  76. private List<string> _PageSizeSelectList = new List<string>() { "10", "20", "30", "40", "50", "100" };
  77. [Description("页面个数选择列表")]
  78. public List<string> PageSizeSelectList
  79. {
  80. get
  81. {
  82. return this._PageSizeSelectList;
  83. }
  84. set
  85. {
  86. this._PageSizeSelectList = value;
  87. }
  88. }
  89. private int _PageIndexSelectDefault = 1;
  90. [Description("默认选择页码")]
  91. public int PageIndexSelectDefault
  92. {
  93. get
  94. {
  95. return this._PageIndexSelectDefault;
  96. }
  97. set
  98. {
  99. this._PageIndexSelectDefault = value;
  100. }
  101. }
  102. [Description("页面分页匹配(2个占位符)")]
  103. public string PageDefault { get; set; } = "{0}/{1}";
  104. [Description("页面默认文本(1个占位符)")]
  105. public string PageInfoDefault { get; set; } = "共 {0} 条记录,每页";
  106. [Description("加载消息文本(2个占位符,1:当前第几页数据,2:已多少记录分页)")]
  107. public string LoadMessage { get; set; } //@"正在加载第 {0} 页数据,以 {1} 条记录进行分页。";
  108. public delegate void PageChange(int PageIndex, int PageSzie);
  109. public void DoOnPageChange(int PageIndex, int PageSzie)
  110. {
  111. if (PageIndex == 0) return;
  112. //if (this.inLoad) return;
  113. if (OnPageChange != null)
  114. {
  115. OnPageChange(PageIndex, PageSzie);
  116. }
  117. }
  118. public event PageChange OnPageChange;
  119. //TODO 这里需要修改到使用方法
  120. public delegate OperateResultInfo ExecActionOperation(int pageIndex, int pageSize);
  121. public delegate string ExecActionMessgae(int pageIndex, int pageSize);
  122. public ExecActionOperation _execActionOperation { get; set; }
  123. public ExecActionMessgae _execActionMessgae { get; set; }
  124. public OperateResultInfo _LastOperation { get; set; }
  125. private void ControlEnable()
  126. {
  127. if (this._PageTotalIndex == 0)
  128. {
  129. BtnBack.Enabled = false;
  130. BtnNext.Enabled = false;
  131. BtnFirst.Enabled = false;
  132. BtnLast.Enabled = false;
  133. return;
  134. }
  135. if (this._pageIndex == 1)
  136. {
  137. BtnBack.Enabled = false;
  138. BtnFirst.Enabled = false;
  139. }
  140. else
  141. {
  142. BtnBack.Enabled = true;
  143. BtnFirst.Enabled = true;
  144. }
  145. if (this._pageIndex == this._PageTotalIndex)
  146. {
  147. BtnNext.Enabled = false;
  148. BtnLast.Enabled = false;
  149. }
  150. else
  151. {
  152. BtnNext.Enabled = true;
  153. BtnLast.Enabled = true;
  154. }
  155. }
  156. #region 分页控件
  157. /// <summary>
  158. /// 每页标签
  159. /// </summary>
  160. KryptonLabel lbEveryPage;
  161. /// <summary>
  162. /// 每页记录数列表控件
  163. /// </summary>
  164. KryptonComboBox cmbEveryPageRecordNum;
  165. /// <summary>
  166. /// 记录标签
  167. /// </summary>
  168. KryptonLabel lbRecordsNum;
  169. /// <summary>
  170. /// 分割线
  171. /// </summary>
  172. KryptonSeparator separator;
  173. /// <summary>
  174. /// 首页按钮
  175. /// </summary>
  176. KryptonButton BtnFirst;
  177. /// <summary>
  178. /// 上一页按钮
  179. /// </summary>
  180. KryptonButton BtnBack;
  181. /// <summary>
  182. /// 当前页标签
  183. /// </summary>
  184. KryptonLabel lbCurrentPage;
  185. /// <summary>
  186. /// 总页数标签
  187. /// </summary>
  188. KryptonLabel lbTotalPage;
  189. /// <summary>
  190. /// 数据记录总数标签
  191. /// </summary>
  192. KryptonLabel lbTotalDataRecords;
  193. /// <summary>
  194. /// 下一页按钮
  195. /// </summary>
  196. KryptonButton BtnNext;
  197. /// <summary>
  198. /// 尾页按钮
  199. /// </summary>
  200. KryptonButton BtnLast;
  201. /// <summary>
  202. /// 分割线
  203. /// </summary>
  204. KryptonSeparator separator1;
  205. /// <summary>
  206. /// 页码标签控件
  207. /// </summary>
  208. KryptonLabel lbPageNum;
  209. /// <summary>
  210. /// 页码列表控件
  211. /// </summary>
  212. KryptonComboBox cmbPageNumEnum;
  213. /// <summary>
  214. /// 分割线
  215. /// </summary>
  216. KryptonSeparator separator2;
  217. /// <summary>
  218. /// 跳转页码按钮
  219. /// </summary>
  220. KryptonButton btGotoPage;
  221. #endregion
  222. private void InitPageControl()
  223. {
  224. #region 分页控件
  225. lbEveryPage = new KryptonLabel();
  226. lbEveryPage.Text = "每页";
  227. lbEveryPage.PaletteMode = PaletteMode.Office2007Silver;
  228. Controls.Add(lbEveryPage);
  229. this.ts_PageControl.Items.Insert(0, new ToolStripControlHost(lbEveryPage));
  230. cmbEveryPageRecordNum = new KryptonComboBox();
  231. cmbEveryPageRecordNum.Height = 23;
  232. cmbEveryPageRecordNum.Width = 40;
  233. cmbEveryPageRecordNum.MinimumSize = new Size(40, 23);
  234. cmbEveryPageRecordNum.PaletteMode = PaletteMode.Office2007Silver;
  235. // cmbEveryPageRecordNum.Items.AddRange(new object[] { 1, 5, 10, 15, 25, 50, 100 });
  236. cmbEveryPageRecordNum.DropDownStyle = ComboBoxStyle.DropDownList;
  237. //cmbEveryPageRecordNum.SelectedIndex = 0;
  238. Controls.Add(cmbEveryPageRecordNum);
  239. this.ts_PageControl.Items.Insert(1, new ToolStripControlHost(cmbEveryPageRecordNum));
  240. lbRecordsNum = new KryptonLabel();
  241. lbRecordsNum.Text = "条记录";
  242. lbRecordsNum.PaletteMode = PaletteMode.Office2007Silver;
  243. Controls.Add(lbRecordsNum);
  244. this.ts_PageControl.Items.Insert(2, new ToolStripControlHost(lbRecordsNum));
  245. separator = new KryptonSeparator();
  246. separator.Height = 25;
  247. separator.Width = 5;
  248. separator.PaletteMode = PaletteMode.Office2007Silver;
  249. Controls.Add(separator);
  250. this.ts_PageControl.Items.Insert(3, new ToolStripControlHost(separator));
  251. BtnFirst = new KryptonButton();
  252. BtnFirst.Text = "首页";
  253. BtnFirst.Height = 25;
  254. BtnFirst.Width = 25;
  255. BtnFirst.AutoSize = true;
  256. BtnFirst.PaletteMode = PaletteMode.Office2007Silver;
  257. Controls.Add(BtnFirst);
  258. this.ts_PageControl.Items.Insert(4, new ToolStripControlHost(BtnFirst));
  259. BtnBack = new KryptonButton();
  260. BtnBack.Text = "上一页";
  261. BtnBack.Height = 25;
  262. BtnBack.Width = 25;
  263. BtnBack.AutoSize = true;
  264. BtnBack.PaletteMode = PaletteMode.Office2007Silver;
  265. Controls.Add(BtnBack);
  266. this.ts_PageControl.Items.Insert(5, new ToolStripControlHost(BtnBack));
  267. lbCurrentPage = new KryptonLabel();
  268. lbCurrentPage.Tag = 1;
  269. lbCurrentPage.Text = "第0页";
  270. lbCurrentPage.PaletteMode = PaletteMode.Office2007Silver;
  271. Controls.Add(lbCurrentPage);
  272. this.ts_PageControl.Items.Insert(6, new ToolStripControlHost(lbCurrentPage));
  273. lbTotalPage = new KryptonLabel();
  274. lbTotalPage.Tag = 10;
  275. lbTotalPage.Text = "共0页";
  276. lbTotalPage.PaletteMode = PaletteMode.Office2007Silver;
  277. Controls.Add(lbTotalPage);
  278. this.ts_PageControl.Items.Insert(7, new ToolStripControlHost(lbTotalPage));
  279. lbTotalDataRecords = new KryptonLabel();
  280. lbTotalDataRecords.Tag = 10;
  281. lbTotalDataRecords.Text = "总计:0条数据记录";
  282. lbTotalDataRecords.PaletteMode = PaletteMode.Office2007Silver;
  283. Controls.Add(lbTotalDataRecords);
  284. this.ts_PageControl.Items.Insert(8, new ToolStripControlHost(lbTotalDataRecords));
  285. BtnNext = new KryptonButton();
  286. BtnNext.Text = "下一页";
  287. BtnNext.Height = 25;
  288. BtnNext.Width = 25;
  289. BtnNext.AutoSize = true;
  290. BtnNext.PaletteMode = PaletteMode.Office2007Silver;
  291. Controls.Add(BtnNext);
  292. this.ts_PageControl.Items.Insert(9, new ToolStripControlHost(BtnNext));
  293. BtnLast = new KryptonButton();
  294. BtnLast.Text = "尾页";
  295. BtnLast.Height = 25;
  296. BtnLast.Width = 25;
  297. BtnLast.AutoSize = true;
  298. BtnLast.PaletteMode = PaletteMode.Office2007Silver;
  299. Controls.Add(BtnLast);
  300. this.ts_PageControl.Items.Insert(10, new ToolStripControlHost(BtnLast));
  301. separator1 = new KryptonSeparator();
  302. separator1.Height = 25;
  303. separator1.Width = 5;
  304. separator1.PaletteMode = PaletteMode.Office2007Silver;
  305. Controls.Add(separator1);
  306. this.ts_PageControl.Items.Insert(11, new ToolStripControlHost(separator1));
  307. lbPageNum = new KryptonLabel();
  308. lbPageNum.Text = "页码:";
  309. Controls.Add(lbPageNum);
  310. this.ts_PageControl.Items.Insert(12, new ToolStripControlHost(lbPageNum));
  311. cmbPageNumEnum = new KryptonComboBox();
  312. cmbPageNumEnum.Height = 23;
  313. cmbPageNumEnum.Width = 70;
  314. cmbPageNumEnum.MinimumSize = new Size(70, 23);
  315. cmbPageNumEnum.PaletteMode = PaletteMode.Office2007Silver;
  316. cmbPageNumEnum.DataSource = this.pages;
  317. Controls.Add(cmbPageNumEnum);
  318. this.ts_PageControl.Items.Insert(13, new ToolStripControlHost(cmbPageNumEnum));
  319. separator2 = new KryptonSeparator();
  320. separator2.Height = 25;
  321. separator2.Width = 5;
  322. separator2.PaletteMode = PaletteMode.Office2007Silver;
  323. Controls.Add(separator2);
  324. this.ts_PageControl.Items.Insert(14, new ToolStripControlHost(separator2));
  325. btGotoPage = new KryptonButton();
  326. btGotoPage.Text = "跳转";
  327. btGotoPage.Height = 25;
  328. btGotoPage.Width = 25;
  329. btGotoPage.AutoSize = true;
  330. btGotoPage.PaletteMode = PaletteMode.Office2007Silver;
  331. Controls.Add(btGotoPage);
  332. this.ts_PageControl.Items.Insert(15, new ToolStripControlHost(btGotoPage));
  333. #endregion
  334. #region 注册按钮事件
  335. cmbEveryPageRecordNum.SelectionChangeCommitted += CmbEveryPageRecordNum_SelectionChangeCommitted; ;
  336. BtnFirst.Click += this.kryptonBtnFirst_Click;
  337. BtnBack.Click += this.kryptonBtnBack_Click;
  338. BtnNext.Click += this.kryptonBtnNext_Click;
  339. BtnLast.Click += this.kryptonBtnLast_Click;
  340. btGotoPage.Click += BtGotoPage_Click;
  341. #endregion
  342. }
  343. private void BtGotoPage_Click(object sender, EventArgs e)
  344. {
  345. var _pageindex = Convert.ToInt32(this.cmbPageNumEnum.SelectedValue);
  346. this.SetPageIndex(_pageindex);
  347. }
  348. private void CmbEveryPageRecordNum_SelectionChangeCommitted(object sender, EventArgs e)
  349. {
  350. PageSize = Convert.ToInt32(cmbEveryPageRecordNum.Text);
  351. }
  352. public PageToolbar()
  353. {
  354. InitializeComponent();
  355. InitPageControl();
  356. this.ControlEnable();
  357. cmbEveryPageRecordNum.DataSource = this.PageSizeSelectList;
  358. cmbPageNumEnum.Text = PageIndexSelectDefault.ToString();
  359. this.pages.ListChanged += Pages_ListChanged;
  360. this.SetPageIndex(1);
  361. }
  362. private void Pages_ListChanged(object sender, ListChangedEventArgs e)
  363. {
  364. }
  365. private void SetPageSize(int pageSize, bool CountChange = false)
  366. {
  367. if (pageSize <= 0)
  368. {
  369. return;
  370. }
  371. var _totlaPages = (int)Math.Ceiling((double)this.DataCount / pageSize);
  372. this._PageTotalIndex = _totlaPages;
  373. this.lbTotalPage.Text = $"共{_totlaPages}页";
  374. this.cmbPageNumEnum.SelectedIndexChanged -= this.CmbEveryPageRecordNum_SelectionChangeCommitted;
  375. this.cmbPageNumEnum.DataSource = null;
  376. lock (this.pages)
  377. {
  378. this.pages.Clear();
  379. for (int n = 1; n <= _PageTotalIndex; n++)
  380. {
  381. this.pages.Add(n);
  382. }
  383. }
  384. this.cmbPageNumEnum.DataSource = pages;
  385. this.cmbPageNumEnum.SelectedIndexChanged += this.CmbEveryPageRecordNum_SelectionChangeCommitted;
  386. this._pageSzie = pageSize;
  387. cmbEveryPageRecordNum.Text = PageSize.ToString();
  388. if (_totlaPages == 0)
  389. {
  390. this._pageIndex = 0;
  391. }
  392. else
  393. {
  394. if (this._pageIndex == 0)
  395. {
  396. this._pageIndex = 1;
  397. }
  398. }
  399. this.SetPageIndex(Math.Min(_totlaPages, this._pageIndex), CountChange);
  400. //if (_pageIndex == 1 || _totlaPages != 0)
  401. //{
  402. // this.lbCurrentPage.Text = $"第{ this._pageIndex}页";
  403. //}
  404. //else if (_pageIndex == 1 || _totlaPages == 0)
  405. //{
  406. // this.lbCurrentPage.Text = $"第0页";
  407. //}
  408. this.ControlEnable();
  409. }
  410. private void SetTotalCount(int totalCount)
  411. {
  412. this._TotalCount = totalCount;
  413. this.lbTotalDataRecords.Text = $"总计:{this._TotalCount}条数据记录";
  414. this.SetPageSize(this.PageSize, true);
  415. }
  416. private bool isIndex = false;
  417. public void SetPageIndex(int pageIndex, bool CountChange = false)
  418. {
  419. if (pageIndex < 0 || pageIndex > this._PageTotalIndex)
  420. {
  421. return;
  422. }
  423. if (isIndex) return;
  424. isIndex = true;
  425. this._pageIndex = pageIndex;
  426. this.lbCurrentPage.Text = $"第{ this._pageIndex}页";
  427. this._PageTotalIndex = (int)Math.Ceiling((double)DataCount / PageSize);
  428. cmbPageNumEnum.Text = PageIndex.ToString();
  429. if (!CountChange)
  430. this.DoOnPageChange(this.PageIndex, this._pageSzie);
  431. isIndex = false;
  432. this.ControlEnable();
  433. }
  434. public void kryptonBtnFirst_Click(object sender, EventArgs e)
  435. {
  436. this.SetPageIndex(1);
  437. }
  438. public void kryptonBtnBack_Click(object sender, EventArgs e)
  439. {
  440. this.SetPageIndex(this._pageIndex - 1);
  441. }
  442. private static object obj = new object();
  443. public void kryptonBtnNext_Click(object sender, EventArgs e)
  444. {
  445. this.SetPageIndex(this._pageIndex + 1);
  446. }
  447. public void kryptonBtnLast_Click(object sender, EventArgs e)
  448. {
  449. this.SetPageIndex(this._PageTotalIndex);
  450. // PageIndex = PageTotalIndex;
  451. }
  452. }
  453. }