123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- // ***********************************************************************
- // Assembly : HZH_Controls
- // Created : 08-15-2019
- //
- // ***********************************************************************
- // <copyright file="UCPagerControl2.cs">
- // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
- // </copyright>
- //
- // 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
- {
- /// <summary>
- /// Class UCPagerControl2.
- /// Implements the <see cref="HZH_Controls.Controls.UCPagerControlBase" />
- /// </summary>
- /// <seealso cref="HZH_Controls.Controls.UCPagerControlBase" />
- [ToolboxItem(true)]
- public partial class UCPagerControl2 : UCPagerControlBase
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="UCPagerControl2" /> class.
- /// </summary>
- public UCPagerControl2()
- {
- InitializeComponent();
- txtPage.txtInput.KeyDown += txtInput_KeyDown;
- }
- /// <summary>
- /// Handles the KeyDown event of the txtInput control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
- void txtInput_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- btnToPage_BtnClick(null, null);
- txtPage.InputText = "";
- }
- }
- /// <summary>
- /// Occurs when [show source changed].
- /// </summary>
- public override event PageControlEventHandler ShowSourceChanged;
- /// <summary>
- /// 关联的数据源
- /// </summary>
- /// <value>The data source.</value>
- public override List<object> DataSource
- {
- get
- {
- return base.DataSource;
- }
- set
- {
- if (value == null)
- {
- base.DataSource = new List<object>();
- }
- else
- {
- base.DataSource = value;
- }
- PageIndex = 1;
- ResetPageCount();
- var s = GetCurrentSource();
- if (ShowSourceChanged != null)
- {
- ShowSourceChanged(s);
- }
- }
- }
- /// <summary>
- /// 每页显示数量
- /// </summary>
- /// <value>The size of the page.</value>
- 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();
- }
- }
- /// <summary>
- /// 第一页
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 上一页
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 下一页
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 最后一页
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// Resets the page count.
- /// </summary>
- 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();
- }
- /// <summary>
- /// Reloads the page.
- /// </summary>
- private void ReloadPage()
- {
- try
- {
- ControlHelper.FreezeControl(tableLayoutPanel1, true);
- List<int> lst = new List<int>();
- 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);
- }
- }
- /// <summary>
- /// Handles the BtnClick event of the page control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- 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);
- }
- }
- }
- /// <summary>
- /// 控制按钮显示
- /// </summary>
- /// <param name="blnLeftBtn">是否显示上一页,第一页</param>
- /// <param name="blnRightBtn">是否显示下一页,最后一页</param>
- protected override void ShowBtn(bool blnLeftBtn, bool blnRightBtn)
- {
- btnFirst.Enabled = btnPrevious.Enabled = blnLeftBtn;
- btnNext.Enabled = btnEnd.Enabled = blnRightBtn;
- }
- /// <summary>
- /// Handles the BtnClick event of the btnFirst control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- private void btnFirst_BtnClick(object sender, EventArgs e)
- {
- FirstPage();
- }
- /// <summary>
- /// Handles the BtnClick event of the btnPrevious control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- private void btnPrevious_BtnClick(object sender, EventArgs e)
- {
- PreviousPage();
- }
- /// <summary>
- /// Handles the BtnClick event of the btnNext control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- private void btnNext_BtnClick(object sender, EventArgs e)
- {
- NextPage();
- }
- /// <summary>
- /// Handles the BtnClick event of the btnEnd control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- private void btnEnd_BtnClick(object sender, EventArgs e)
- {
- EndPage();
- }
- /// <summary>
- /// Handles the BtnClick event of the btnToPage control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- 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);
- }
- }
- }
- }
- }
- }
|