Files

229 lines
4.8 KiB
C#

using NaiBa.Util.Extension;
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 TemuKJD.UserControls
{
public partial class UcSplitPage : UserControl
{
/// <summary>
/// 是否显示每页条数(启用每页条数功能)
/// </summary>
[Description("显示每页条数"), Category("UserProperty")]
public bool IsShowPageSize
{
get
{
return this.cmbPageSize.Visible;
}
set
{
if (cmbPageSize.Visible != value)
{
this.lblPageSize.Visible = this.cmbPageSize.Visible = value;
this.Invalidate();
}
}
}
int maxPageSizeValue = 5000;
/// <summary>
/// 是否显示每页条数(启用每页条数功能)
/// </summary>
[Description("每页条数最大值"), Category("UserProperty")]
public int MaxPageSizeValue
{
get
{
return maxPageSizeValue;
}
set
{
if (maxPageSizeValue != value)
{
maxPageSizeValue = value;
foreach (var item in new string[] { "50", "100","200","500","1000","2000","5000" })
{
if (item.ParseToInt() > maxPageSizeValue)
this.cmbPageSize.Items.Remove(item);
}
this.Invalidate();
}
}
}
/// <summary>
/// 是否显示总条数
/// </summary>
[Description("显示总条数"), Category("UserProperty")]
public bool IsShowTotalCount
{
get
{
return this.lblTotal.Visible;
}
set
{
if (lblTotal.Visible != value)
{
this.lblTotal.Visible = value;
this.Invalidate();
}
}
}
long totalCount = 0;
/// <summary>
/// 总数量
/// </summary>
[Description("总数量"), Category("UserProperty")]
public long TotalCount
{
get { return totalCount; }
set
{
if (totalCount != value && value >= 0)
{
totalCount = value;
// 根据总数量和每页条数计算总页数
int pageSize = this.PageSize;
if (pageSize > 0)
{
this.TotalPage = (int)Math.Ceiling((double)totalCount / pageSize);
}
else
{
this.TotalPage = 1;
}
Refresh();
}
}
}
int totalPage = 1;
public int TotalPage
{
get { return totalPage; }
set
{
if (totalPage != value && value > 0)
{
totalPage = value;
Refresh();
}
}
}
int currentPage = 1;
public int CurrentPage
{
get { return currentPage; }
set
{
// 确保当前页不超过总页数
int newPage = Math.Max(1, Math.Min(totalPage, value));
if (currentPage != newPage)
{
currentPage = newPage;
Refresh();
}
OnPageChange(this);
}
}
[Description("每页条数"), Category("UserProperty")]
public int PageSize
{
get
{
int pageSize = 50;
if (InvokeRequired)
{
Invoke(new Action(() =>
{
pageSize = Math.Max(this.cmbPageSize.Text.ParseToInt(), 1);
}));
}
else
pageSize = Math.Max(this.cmbPageSize.Text.ParseToInt(), 1);
return pageSize;
}
set
{
if (InvokeRequired)
{
Invoke(new Action(() =>
{
if (this.cmbPageSize.Text.ParseToInt() != value)
this.cmbPageSize.Text = value.ParseToString();
else
this.cmbPageSize.Text = value.ParseToString();
}));
}
else
{
if (this.cmbPageSize.Text.ParseToInt() != value)
this.cmbPageSize.Text = value.ParseToString();
else
this.cmbPageSize.Text = value.ParseToString();
}
// 当每页条数变化时,重新计算总页数
if (totalCount > 0)
{
this.TotalPage = (int)Math.Ceiling((double)totalCount / value);
// 确保当前页不超过新的总页数
if (this.CurrentPage > this.TotalPage)
{
this.CurrentPage = this.TotalPage;
}
}
//OnPageChange(this);
}
}
public event Action<object> PageChange;
protected virtual void OnPageChange(object sender)
{
this.PageChange?.Invoke(sender);
}
public UcSplitPage()
{
InitializeComponent();
}
private void pnlJump_Click(object sender, EventArgs e)
{
int page = this.txtJumpPage.Text.ParseToInt();
this.CurrentPage = Math.Max(1, page);
}
private void btnPrev_Click(object sender, EventArgs e)
{
int page = this.CurrentPage - 1;
this.CurrentPage = Math.Max(1, page);
}
private void btnNext_Click(object sender, EventArgs e)
{
int page = this.CurrentPage + 1;
this.CurrentPage = Math.Min(totalPage, page);
}
private void Refresh()
{
Invoke(new Action(() =>
{
lblPage.Text = $"{currentPage}/{TotalPage}页";
lblTotal.Text = $"共{TotalCount}条";
}));
}
}
}