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; }
///
/// 计算总页数
///
///
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;
}
///
/// 首页
///
///
///
private void linkFirst_LinkClicked(object sender, EventArgs e)
{
PageIndex = 1;
DrawControl(true);
}
///
/// 上一页
///
///
///
private void linkPrev_LinkClicked(object sender, EventArgs e)
{
PageIndex = Math.Max(1, PageIndex - 1);
DrawControl(true);
}
///
/// 下一页
///
///
///
private void linkNext_LinkClicked(object sender, EventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + 1);
DrawControl(true);
}
///
/// 末页
///
///
///
private void linkLast_LinkClicked(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
}
public void DrawControl(int count)
{
TotalCount = count;
DrawControl(false);
}
///
/// 页面控件呈现
///
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;
}
///
/// enter键功能
///
private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
{
linkGo_LinkClicked(null, null);
}
///
/// 跳转页数限制
///
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();
}
}
}
///
/// 跳转指定页
///
///
///
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);//底边
}
///
/// 每页显示记录数更改事件
///
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);
}
}
}
}