You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.3 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.Windows.Forms;
using JwKdsV.Core;
using System.Threading;
namespace JwKdsV.Component
{
public partial class PagerControl : UserControl
{
public PagerControl()
{
InitializeComponent();
}
private int _totalCount = 0;
/// <summary>
/// 总条数
/// </summary>
[Category("杂项"), Description("总条数")]
public int TotalCount
{
get
{
return _totalCount;
}
set
{
if(_totalCount == value)
{
return;
}
_totalCount = value;
CalculatePageCount();
}
}
private int _pageSize = 5;
/// <summary>
/// 每页展示数量
/// </summary>
[Category("杂项"), Description("每页展示数量")]
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = value;
CalculatePageCount();
}
}
/// <summary>
/// 总页数
/// </summary>
[Category("杂项"), Description("总页数")]
public int PageCount { get; set; }
private int _currentPage;
/// <summary>
/// 当前页数
/// </summary>
[Category("杂项"), Description("当前页数")]
public int CurrentPage
{
get
{
return _currentPage;
}
set
{
if(_currentPage == value)
{
return;
}
_currentPage = value;
RefreshUI();
}
}
private void CalculatePageCount()
{
if(PageSize == 0)
{
//排除错误设置
return;
}
if(TotalCount % PageSize == 0)
{
PageCount = TotalCount / PageSize;
}
else
{
PageCount = TotalCount / PageSize + 1;
}
//默认显示第一页
this.CurrentPage = 1;
RefreshUI();
}
/// <summary>
/// 更新UI
/// </summary>
private void RefreshUI()
{
this.upButton.Enabled = true;
this.downButton.Enabled = true;
if(CurrentPage <= 1)
{
this.upButton.Enabled = false;
}
if(CurrentPage >= PageCount)
{
this.downButton.Enabled = false;
}
this.pageLabel.Text = string.Format("{0}/{1}", CurrentPage, PageCount);
}
private Font _font = Constant.NORMAL_FONT;
[Category("杂项"), Description("字体")]
public Font PanelFont
{
get
{
return this._font;
}
set
{
this._font = value;
this.upButton.Font = this._font;
this.pageLabel.Font = this._font;
this.downButton.Font = this._font;
}
}
private void OnPageChangeClick(object sender, EventArgs e)
{
if(sender is Button)
{
var obj = sender as Button;
var target = obj.Tag as string;
if(target == "up")
{
//上一页
this.CurrentPage--;
}
else
{
//下一页
this.CurrentPage++;
}
OnPageChange(this);
}
}
public event Action<object> PageChangeWithObject;
private void OnPageChange(PagerControl sender)
{
Action<object> temp = Interlocked.CompareExchange(ref PageChangeWithObject, null, null);
if (temp != null)
{
temp(sender);
}
}
}
}