Files
29WxOrder/29WxOrder/MyControl/UcPager.cs
T
2026-06-11 15:17:50 +08:00

175 lines
6.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NaiBa.Util.Extension;
namespace _29WxOrder.MyControl
{
public partial class UcPager : UserControl
{
public UcPager()
{
InitializeComponent();
}
private void UcPager_Load(object sender, EventArgs e)
{
ShowPageNumber();
}
public delegate void PageTurnEvent(object sender, int page);
public event PageTurnEvent PageTurned;
public int TotalCount { get; set; } = -1;
int totalPage = 1;
public int TotalPage { get { return totalPage; } set { totalPage = value; ShowPageNumber(); } }
int currentPage = 1;
public int CurrentPage { get { return currentPage; } set { currentPage = value; ShowPageNumber(); } }
private void lblPage_Click(object sender, EventArgs e)
{
var label = sender as Label;
if (label == null)
return;
if (label.Cursor != Cursors.Hand)
return;
if (label.Text == "尾页")
currentPage = TotalPage;
else if (label.Text == "首页")
currentPage = 1;
else if (label.Text == "▷")
currentPage += 1;
else if (label.Text == "◁")
currentPage -= 1;
else
currentPage = Convert.ToInt32(label.Text);
ShowPageNumber();
PageTurned(this, currentPage);
}
/// <summary>
/// 显示分页页码
/// </summary>
void ShowPageNumber()
{
if (currentPage > totalPage)
currentPage = totalPage;
if (currentPage < 1)
currentPage = 1;
int startPage;
if (currentPage + 4 >= totalPage)
startPage = totalPage - 8;
else
startPage = currentPage - 4;
if (startPage < 1)
startPage = 1;
var endPage = startPage + 8;
if (endPage > totalPage)
endPage = totalPage;
lblTotal.Text = "共 " + totalPage + " 页" + (TotalCount >= 0 ? " " + TotalCount + " 条数据" : "");
if (totalPage >= 2)
{
lblFirstPage.Visible = lblLastPage.Visible = lblPrev.Visible = lblNext.Visible = txtJumpPage.Visible = lblJump.Visible = true;
if (currentPage == 1)
{
lblFirstPage.Font = new Font(lblFirstPage.Font.FontFamily, lblFirstPage.Font.Size);
lblFirstPage.Cursor = Cursors.Default;
lblFirstPage.ForeColor = Color.Black;
lblPrev.Font = new Font(lblPrev.Font.FontFamily, lblPrev.Font.Size);
lblPrev.Cursor = Cursors.Default;
lblPrev.ForeColor = Color.Black;
}
else
{
lblFirstPage.Font = new Font(lblFirstPage.Font.FontFamily, lblFirstPage.Font.Size, FontStyle.Underline);
lblFirstPage.Cursor = Cursors.Hand;
lblFirstPage.ForeColor = Color.Blue;
lblPrev.Font = new Font(lblPrev.Font.FontFamily, lblPrev.Font.Size, FontStyle.Underline);
lblPrev.Cursor = Cursors.Hand;
lblPrev.ForeColor = Color.Blue;
}
if (currentPage == totalPage)
{
lblLastPage.Font = new Font(lblFirstPage.Font.FontFamily, lblFirstPage.Font.Size);
lblLastPage.Cursor = Cursors.Default;
lblLastPage.ForeColor = Color.Black;
lblNext.Font = new Font(lblPrev.Font.FontFamily, lblPrev.Font.Size);
lblNext.Cursor = Cursors.Default;
lblNext.ForeColor = Color.Black;
}
else
{
lblLastPage.Font = new Font(lblFirstPage.Font.FontFamily, lblFirstPage.Font.Size, FontStyle.Underline);
lblLastPage.Cursor = Cursors.Hand;
lblLastPage.ForeColor = Color.Blue;
lblNext.Font = new Font(lblPrev.Font.FontFamily, lblPrev.Font.Size, FontStyle.Underline);
lblNext.Cursor = Cursors.Hand;
lblNext.ForeColor = Color.Blue;
}
}
else
{
lblFirstPage.Visible = lblLastPage.Visible = lblPrev.Visible = lblNext.Visible /*= txtJumpPage.Visible = lblJump.Visible*/ = false;
}
// 初始化按钮数量
while (pnlPage.Controls.Count <= endPage - startPage)
{
var l = new Label() { TextAlign = ContentAlignment.MiddleCenter, };
l.Click += lblPage_Click;
pnlPage.Controls.Add(l);
}
// 按钮偏移位置计数
int x = (int)(lblNext.Font.Size / 2);
// 调整按钮位置等
for (int i = startPage; i < startPage + pnlPage.Controls.Count; i++)
{
Label lbl = pnlPage.Controls[i - startPage] as Label;
if (i > endPage)
{
lbl.Visible = false;
continue;
}
lbl.MinimumSize = new Size(20, (int)lblNext.Font.Size + 6);
lbl.Visible = true;
lbl.AutoSize = true;
lbl.Text = i.ToString();
lbl.Name = "lbl" + i.ToString();
lbl.Location = new Point(x, lbl.Location.Y);
if (i == currentPage)
{
lbl.Font = new Font("Microft YaHei", lbl.Font.Size);
lbl.Cursor = Cursors.Default;
lbl.ForeColor = Color.Black;
}
else
{
lbl.Font = new Font("Microft YaHei", lbl.Font.Size, FontStyle.Underline);
lbl.Cursor = Cursors.Hand;
lbl.ForeColor = Color.Blue;
}
x += lbl.Width + 10;
}
}
private void lblJump_Click(object sender, EventArgs e)
{
this.currentPage = this.txtJumpPage.Text.ParseToInt();
ShowPageNumber();
PageTurned(this, currentPage);
}
}
}