123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using DevExpress.XtraEditors;
- using DevExpress.XtraPrinting;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace XdCxRhDW.App.UserControl
- {
- public partial class UcCtrlPage : DevExpress.XtraEditors.XtraUserControl
- {
- public event EventHandler OnPageChanged;
- public UcCtrlPage()
- {
- InitializeComponent();
- }
- //每页显示记录数
- private int pageSize = 10;
- //当前页
- private int pageIndex = 1;
- //总页数
- private int pageCount = 0;
- //总记录数
- private int totalCount = 0;
- public int PageSize { get => pageSize; set => pageSize = value; }
- public int PageIndex { get => pageIndex; set => pageIndex = value; }
- public int PageCount
- {
- get
- {
- if (pageSize != 0)
- {
- pageCount = GetPageCount();
- }
- return pageCount;
- }
- set { pageCount = value; }
- }
- public int TotalCount { get => totalCount; set => totalCount = value; }
- /// <summary>
- /// 计算总页数
- /// </summary>
- /// <returns></returns>
- private int GetPageCount()
- {
- if (PageSize == 0)
- {
- return 0;
- }
- int pageCount = TotalCount / PageSize;
- if (TotalCount % PageSize == 0)
- {
- pageCount = TotalCount / PageSize;
- }
- else
- {
- pageCount = TotalCount / PageSize + 1;
- }
- return pageCount;
- }
- /// <summary>
- /// 首页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkFirst_LinkClicked(object sender, EventArgs e)
- {
- PageIndex = 1;
- DrawControl(true);
- }
- /// <summary>
- /// 上一页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkPrev_LinkClicked(object sender, EventArgs e)
- {
- PageIndex = Math.Max(1, PageIndex - 1);
- DrawControl(true);
- }
- /// <summary>
- /// 下一页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkNext_LinkClicked(object sender, EventArgs e)
- {
- PageIndex = Math.Min(PageCount, PageIndex + 1);
- DrawControl(true);
- }
- /// <summary>
- /// 末页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkLast_LinkClicked(object sender, EventArgs e)
- {
- PageIndex = PageCount;
- DrawControl(true);
- }
- public void DrawControl(int count)
- {
- TotalCount = count;
- DrawControl(false);
- }
- /// <summary>
- /// 页面控件呈现
- /// </summary>
- private void DrawControl(bool callEvent)
- {
- lblTotalPage.Text = new StringBuilder("第 ").Append(PageIndex.ToString()).Append(" / ").Append(PageCount.ToString()).Append(" 页").ToString();
- lblTotalCount.Text = new StringBuilder("共 ").Append(TotalCount.ToString()).Append(" 条").ToString();
- cbbPageSize.Text = PageSize.ToString();
- if (callEvent && OnPageChanged != null)
- {
- OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
- }
- SetFormCtrEnabled();
- if (PageCount <= 1)//有且仅有一页
- {
- linkFirst.Enabled = false;
- linkPrev.Enabled = false;
- linkNext.Enabled = false;
- linkLast.Enabled = false;
- linkGo.Enabled = false;
- }
- else if (PageIndex == 1)//第一页
- {
- linkFirst.Enabled = false;
- linkPrev.Enabled = false;
- }
- else if (PageIndex == PageCount)//最后一页
- {
- linkNext.Enabled = false;
- linkLast.Enabled = false;
- }
- }
- private void SetFormCtrEnabled()
- {
- linkFirst.Enabled = true;
- linkPrev.Enabled = true;
- linkNext.Enabled = true;
- linkLast.Enabled = true;
- linkGo.Enabled = true;
- }
- /// <summary>
- /// enter键功能
- /// </summary>
- private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
- {
- linkGo_LinkClicked(null, null);
- }
- /// <summary>
- /// 跳转页数限制
- /// </summary>
- private void txtPageNum_TextChanged(object sender, EventArgs e)
- {
- int num = 0;
- if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
- {
- if (num > PageCount)
- {
- txtPageNum.Text = PageCount.ToString();
- }
- }
- }
- /// <summary>
- /// 跳转指定页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkGo_LinkClicked(object sender, EventArgs e)
- {
- int num = 0;
- if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
- {
- PageIndex = num;
- DrawControl(true);
- }
- }
- private void PageControl_Paint(object sender, PaintEventArgs e)
- {
- drawBorder(e);
- }
- private void drawBorder(PaintEventArgs e)
- {
- Color _lineColor = Color.FromArgb(0, 123, 255);
- ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
- _lineColor, 1, ButtonBorderStyle.Solid, //左边
- _lineColor, 1, ButtonBorderStyle.Solid, //上边
- _lineColor, 1, ButtonBorderStyle.Solid, //右边
- _lineColor, 1, ButtonBorderStyle.Solid);//底边
- }
- /// <summary>
- /// 每页显示记录数更改事件
- /// </summary>
- bool isTextChanged = false;
- private void cbbPageSize_SelectedIndexChanged(object sender, EventArgs e)
- {
- int num = 0;
- if (!int.TryParse(cbbPageSize.Text.Trim(), out num) || num <= 0)
- {
- num = 10;
- cbbPageSize.Text = "10";
- }
- else
- {
- isTextChanged = true;
- }
- pageSize = num;
- if (isTextChanged)
- {
- isTextChanged = false;
- linkFirst_LinkClicked(null, null);
- }
- }
- }
- }
|