// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-15-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
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;
namespace HZH_Controls.Controls
{
///
/// Class UCPagerControl2.
/// Implements the
///
///
[ToolboxItem(true)]
public partial class UCPagerControl2 : UCPagerControlBase
{
///
/// Initializes a new instance of the class.
///
public UCPagerControl2()
{
InitializeComponent();
txtPage.txtInput.KeyDown += txtInput_KeyDown;
}
///
/// Handles the KeyDown event of the txtInput control.
///
/// The source of the event.
/// The instance containing the event data.
void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnToPage_BtnClick(null, null);
txtPage.InputText = "";
}
}
///
/// Occurs when [show source changed].
///
public override event PageControlEventHandler ShowSourceChanged;
///
/// 关联的数据源
///
/// The data source.
public override List DataSource
{
get
{
return base.DataSource;
}
set
{
if (value == null)
{
base.DataSource = new List();
}
else
{
base.DataSource = value;
}
PageIndex = 1;
ResetPageCount();
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
}
///
/// 每页显示数量
///
/// The size of the page.
public override int PageSize
{
get
{
return base.PageSize;
}
set
{
base.PageSize = value;
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
ResetPageCount();
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
}
public override int PageCount
{
get
{
return base.PageCount;
}
set
{
base.PageCount = value;
ReloadPage();
}
}
public override int PageIndex
{
get
{
return base.PageIndex;
}
set
{
base.PageIndex = value;
ReloadPage();
}
}
///
/// 第一页
///
public override void FirstPage()
{
if (PageIndex == 1)
return;
PageIndex = 1;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
StartIndex = (PageIndex - 1) * PageSize;
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
///
/// 上一页
///
public override void PreviousPage()
{
if (PageIndex <= 1)
{
return;
}
PageIndex--;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
StartIndex = (PageIndex - 1) * PageSize;
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
///
/// 下一页
///
public override void NextPage()
{
if (PageIndex >= PageCount)
{
return;
}
PageIndex++;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
StartIndex = (PageIndex - 1) * PageSize;
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
///
/// 最后一页
///
public override void EndPage()
{
if (PageIndex == PageCount)
return;
PageIndex = PageCount;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
StartIndex = (PageIndex - 1) * PageSize;
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
///
/// Resets the page count.
///
private void ResetPageCount()
{
if (PageSize > 0)
{
if (base.DataSource != null)
PageCount = base.DataSource.Count / base.PageSize + (base.DataSource.Count % base.PageSize > 0 ? 1 : 0);
}
txtPage.MaxValue = PageCount;
txtPage.MinValue = 1;
ReloadPage();
}
///
/// Reloads the page.
///
private void ReloadPage()
{
try
{
ControlHelper.FreezeControl(tableLayoutPanel1, true);
List lst = new List();
if (PageCount <= 9)
{
for (var i = 1; i <= PageCount; i++)
{
lst.Add(i);
}
}
else
{
if (this.PageIndex <= 6)
{
for (var i = 1; i <= 7; i++)
{
lst.Add(i);
}
lst.Add(-1);
lst.Add(PageCount);
}
else if (this.PageIndex > PageCount - 6)
{
lst.Add(1);
lst.Add(-1);
for (var i = PageCount - 6; i <= PageCount; i++)
{
lst.Add(i);
}
}
else
{
lst.Add(1);
lst.Add(-1);
var begin = PageIndex - 2;
var end = PageIndex + 2;
if (end > PageCount)
{
end = PageCount;
begin = end - 4;
if (PageIndex - begin < 2)
{
begin = begin - 1;
}
}
else if (end + 1 == PageCount)
{
end = PageCount;
}
for (var i = begin; i <= end; i++)
{
lst.Add(i);
}
if (end != PageCount)
{
lst.Add(-1);
lst.Add(PageCount);
}
}
}
for (int i = 0; i < 9; i++)
{
UCBtnExt c = (UCBtnExt)this.tableLayoutPanel1.Controls.Find("p" + (i + 1), false)[0];
if (i >= lst.Count)
{
c.Visible = false;
}
else
{
if (lst[i] == -1)
{
c.BtnText = "...";
c.Enabled = false;
}
else
{
c.BtnText = lst[i].ToString();
c.Enabled = true;
}
c.Visible = true;
if (lst[i] == PageIndex)
{
c.RectColor = Color.FromArgb(255, 77, 59);
}
else
{
c.RectColor = Color.FromArgb(223, 223, 223);
}
}
}
ShowBtn(PageIndex > 1, PageIndex < PageCount);
}
finally
{
ControlHelper.FreezeControl(tableLayoutPanel1, false);
}
}
///
/// Handles the BtnClick event of the page control.
///
/// The source of the event.
/// The instance containing the event data.
private void page_BtnClick(object sender, EventArgs e)
{
PageIndex = (sender as UCBtnExt).BtnText.ToInt();
StartIndex = (PageIndex - 1) * PageSize;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
///
/// 控制按钮显示
///
/// 是否显示上一页,第一页
/// 是否显示下一页,最后一页
protected override void ShowBtn(bool blnLeftBtn, bool blnRightBtn)
{
btnFirst.Enabled = btnPrevious.Enabled = blnLeftBtn;
btnNext.Enabled = btnEnd.Enabled = blnRightBtn;
}
///
/// Handles the BtnClick event of the btnFirst control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnFirst_BtnClick(object sender, EventArgs e)
{
FirstPage();
}
///
/// Handles the BtnClick event of the btnPrevious control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnPrevious_BtnClick(object sender, EventArgs e)
{
PreviousPage();
}
///
/// Handles the BtnClick event of the btnNext control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnNext_BtnClick(object sender, EventArgs e)
{
NextPage();
}
///
/// Handles the BtnClick event of the btnEnd control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnEnd_BtnClick(object sender, EventArgs e)
{
EndPage();
}
///
/// Handles the BtnClick event of the btnToPage control.
///
/// The source of the event.
/// The instance containing the event data.
private void btnToPage_BtnClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtPage.InputText))
{
PageIndex = txtPage.InputText.ToInt();
StartIndex = (PageIndex - 1) * PageSize;
ReloadPage();
if (PageModel == HZH_Controls.Controls.PageModel.Soure)
{
var s = GetCurrentSource();
if (ShowSourceChanged != null)
{
ShowSourceChanged(s);
}
}
else
{
if (ShowSourceChanged != null)
{
ShowSourceChanged(this);
}
}
}
}
}
}