现在的位置: 首页 > 综合 > 正文

自己写的Winform分页控件

2013年12月02日 ⁄ 综合 ⁄ 共 7913字 ⁄ 字号 评论关闭

 

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 PageDemo.Controls
{
    public partial class Pager : UserControl
    {
        public Pager()
        {
            InitializeComponent();
            BindEvents();
            Onit();
        }

        #region Field

        /// <summary>
        /// 总页数
        /// </summary>
        private int PageCount;

        /// <summary>
        /// 页码内容列表
        /// </summary>
        private IList<String> PageIndexes;

        #endregion

        #region Property

        /// <summary>
        /// 当前页码
        /// </summary>
        private int _CurrentPage;
        public int CurrentPage
        {
            get
            {
                return _CurrentPage;
            }
            set
            {
                _CurrentPage = value;
                ShowPagesLinkList();
            }
        }

        /// <summary>
        /// 每页记录条数
        /// </summary>
        private int _PageSize;
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
                ShowPagesLinkList();
            }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        private int _RecordCount;
        public int RecordCount
        {
            get
            {
                return _RecordCount;
            }
            set
            {
                _RecordCount = value;
                ShowPagesLinkList();
            }
        }

        #endregion

        #region EventProperty

        /// <summary>
        /// 页面更改事件
        /// </summary>
        public EventHandler PageIndexChanged;

        #endregion

        #region Event

        /// <summary>
        /// LinkLabel单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LinkButtonClick(object sender, EventArgs e)
        {
            LinkLabel linkLabel = (LinkLabel)sender;
            if (linkLabel.Text != "...")
            {
                switch (linkLabel.Text)
                {
                    case "上一页":
                        CurrentPage--;
                        break;
                    case "下一页":
                        CurrentPage++;
                        break;
                    default:
                        CurrentPage = int.Parse(linkLabel.Text);
                        break;
                }

                if (PageIndexChanged != null)
                {
                    PageIndexChanged(this, new EventArgs());
                }
            }
        }

        /// <summary>
        /// 前往指定页
        /// </summary>
        private void GotoDestinationPage(object sender, EventArgs e)
        {
            int result = 0;
            if (int.TryParse(txtDesPage.Text, out result))
            {
                if (result <= 0)
                {
                    CurrentPage = 1;
                }
                else if (result >= PageCount)
                {
                    CurrentPage = PageCount;
                }
                else
                {
                    CurrentPage = result;
                }
            }
            PageIndexChanged(this, new EventArgs());
        }

        #endregion

        #region HelpMethod

        /// <summary>
        /// 得到页码链接列表
        /// </summary>
        private void ShowPagesLinkList()
        {
            PageIndexes = new List<string>();
            int startPage = 1;
            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }
            lblPageCount.Text = PageCount.ToString();
            lblRecordCount.Text = RecordCount.ToString();
            txtDesPage.Text = CurrentPage.ToString();

            if (PageCount <= 10)
            {
                for (int i = startPage; i <= PageCount; i++)
                {
                    PageIndexes.Add(i.ToString());
                }
                for (int i = PageCount + 1; i <= 11; i++)
                {
                    PageIndexes.Add(string.Empty);
                }
            }
            else if (CurrentPage <= (1 + 5))//左半边显示全部页码,右半边显示省略号
            {
                for (int i = startPage; i <= 8; i++)
                {
                    PageIndexes.Add(i.ToString());
                }

                PageIndexes.Add("...");
                PageIndexes.Add(PageCount.ToString());
                PageIndexes.Add(string.Empty);
            }
            else if (CurrentPage >= (PageCount - 5))//左半边显示省略号,右半边显示全部页码
            {
                PageIndexes.Add("1");
                PageIndexes.Add("...");

                for (int i = PageCount - 8; i <= PageCount; i++)
                {
                    PageIndexes.Add(i.ToString());
                }
                PageIndexes.Add(string.Empty);
            }
            else//两边都显示省略号
            {
                PageIndexes.Add("1");
                PageIndexes.Add("...");

                for (int i = CurrentPage - 3; i <= CurrentPage+3; i++)
                {
                    PageIndexes.Add(i.ToString());
                }

                PageIndexes.Add("...");
                PageIndexes.Add(PageCount.ToString());
            }

            ChangeShowingPageIndex();
            ChangeLinkButtonState();
        }

        /// <summary>
        /// 改变页码显示控件内容
        /// </summary>
        private void ChangeShowingPageIndex()
        {
            linkLabel1.Text = PageIndexes[0];
            ChangeLinLabelStyle(linkLabel1);

            linkLabel2.Text = PageIndexes[1];
            ChangeLinLabelStyle(linkLabel2);

            linkLabel3.Text = PageIndexes[2];
            ChangeLinLabelStyle(linkLabel3);

            linkLabel4.Text = PageIndexes[3];
            ChangeLinLabelStyle(linkLabel4);

            linkLabel5.Text = PageIndexes[4];
            ChangeLinLabelStyle(linkLabel5);

            linkLabel6.Text = PageIndexes[5];
            ChangeLinLabelStyle(linkLabel6);

            linkLabel7.Text = PageIndexes[6];
            ChangeLinLabelStyle(linkLabel7);

            linkLabel8.Text = PageIndexes[7];
            ChangeLinLabelStyle(linkLabel8);

            linkLabel9.Text = PageIndexes[8];
            ChangeLinLabelStyle(linkLabel9);

            linkLabel10.Text = PageIndexes[9];
            ChangeLinLabelStyle(linkLabel10);

            linkLabel11.Text = PageIndexes[10];
            ChangeLinLabelStyle(linkLabel11);
        }

        /// <summary>
        /// 根据控件文本改变显示样式
        /// </summary>
        /// <param name="linkLabel"></param>
        private void ChangeLinLabelStyle(LinkLabel linkLabel)
        {
            linkLabel.BackColor = SystemColors.Control;
            if (linkLabel.Text == "...")
            {
                linkLabel.LinkBehavior = LinkBehavior.NeverUnderline;
            }
            else
            {
                int result = 0;
                if(int.TryParse(linkLabel.Text,out result))
                {
                    if (result == CurrentPage)
                    {
                        linkLabel.BackColor = Color.Aqua;
                    }
                }
                linkLabel.LinkBehavior = LinkBehavior.AlwaysUnderline;
            }
        }

        /// <summary>
        /// 为已有控件添加事件绑定
        /// </summary>
        private void BindEvents()
        {
            llblPre.Click += new EventHandler(LinkButtonClick);
            llblNext.Click += new EventHandler(LinkButtonClick);
            btnGo.Click += new EventHandler(GotoDestinationPage);
        }

        /// <summary>
        /// 判断上一页下一页按钮是否显示
        /// </summary>
        private void ChangeLinkButtonState()
        {
            llblPre.Visible = true;
            llblNext.Visible = true;
            if (CurrentPage == 1)
            {
                llblPre.Visible = false;
            }
            if (CurrentPage == PageCount)
            {
                llblNext.Visible = false;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Onit()
        {
            _CurrentPage = 1;
            _PageSize = 10;
            PageCount = 0;
            PageIndexes = new List<string>();
            foreach (Control control in panel1.Controls)
            {
                if (control is LinkLabel)
                {
                    control.Click += new EventHandler(LinkButtonClick);
                }
            }
        }

        #endregion
    }
}

抱歉!评论已关闭.