天天看點

分享一個自己寫的winform版的分頁使用者控件

由于本人寫作能力很差,是以這裡就隻寫代碼了......哈哈哈

下面是分頁使用者控件的所有代碼

public partial class PageNumControl : UserControl
    {
        public PageNumControl()
        {
            InitializeComponent();
        }

        #region 分頁字段和屬性

        private int pageIndex = 1;
        /// <summary>
        /// 目前頁碼
        /// </summary>
        [Description("目前頁碼")]
        public virtual int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        private int pageSize = 15;
        /// <summary>
        /// 每頁顯示記錄數
        /// </summary>
        [Description("每頁顯示記錄數")]
        public virtual int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        public int EndIndex
        {
            get
            {
                return this.PageIndex * this.PageSize;
            }
        }

        public int Startindex
        {
            get
            {
                return (this.PageIndex - 1) * this.PageSize + 1;
            }
        }

        /// <summary>
        /// 頁碼偏移量
        /// </summary>
        [Description("頁碼偏移量")]
        public int PageOffset { get; set; }

        /// <summary>
        /// 顯示頁碼條數
        /// </summary>
        [Description("顯示頁碼條數")]
        public int PageBarNum { get; set; } = 10;

        /// <summary>
        /// 總記錄數
        /// </summary>
        private int recordCount = 0;

        /// <summary>
        /// 除目前頁碼其他頁碼字型
        /// </summary>
        [Description("除目前頁碼其他頁碼字型")]
        public Font NumFont { get; set; } = new System.Drawing.Font("宋體", 9F);

        /// <summary>
        /// 目前頁碼字型
        /// </summary>
        [Description("目前頁碼字型")]
        public Font CurrentNumFont { get; set; } = new System.Drawing.Font("宋體", 9F);

        /// <summary>
        /// 除目前頁碼其他頁碼顔色
        /// </summary>
        [Description("除目前頁碼其他頁碼顔色")]
        public Color NumColor { get; set; } = Color.Black;

        /// <summary>
        /// 目前頁碼顔色
        /// </summary>
        [Description("目前頁碼顔色")]
        public Color CurrentNumColor { get; set; } = Color.Red;

        /// <summary>
        /// 尾部顯示内容
        /// </summary>
        [Description("尾部顯示内容")]
        public string LastShowText { get; set; }

        /// <summary>
        /// 總記錄數
        /// </summary>
        [Description("總記錄數")]
        public virtual int RecordCount
        {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private int pageCount = 0;
        /// <summary>
        /// 總頁數
        /// </summary>
        [Description("總頁數")]
        public int PageCount
        {
            get
            {
                if (pageSize != 0)
                {
                    pageCount = GetPageCount();
                }
                return pageCount;
            }
        }
        /// <summary>
        /// 計算總頁數
        /// </summary>
        /// <returns></returns>
        private int GetPageCount()
        {
            if (PageSize == 0)
            {
                return 0;
            }
            int pageCount = RecordCount / PageSize;
            if (RecordCount % PageSize == 0)
            {
                pageCount = RecordCount / PageSize;
            }
            else
            {
                pageCount = RecordCount / PageSize + 1;
            }
            return pageCount;
        }

        /// <summary>
        /// 每個頁碼之間的距離長度
        /// </summary>
        [Description("頁碼間隔長度")]
        public int fwd { get; set; } = 10;

        /// <summary>
        /// 第一個子產品距離左邊的距離
        /// </summary>
        [Description("第一個子產品間隔長度")]
        public int FirstXLocation { get; set; } = 10;

        /// <summary>
        /// 頁碼y軸距離
        /// </summary>
        [Description("頁碼y軸距離")]
        public int YLocation { get; set; } = 28;

        /// <summary>
        /// 頁碼寬度
        /// </summary>
        [Description("頁碼寬度")]
        public int PageWidth { get; set; } = 179;

        /// <summary>
        /// 頁碼高度
        /// </summary>
        [Description("頁碼高度")]
        public int PageHeight { get; set; } = 12;

        /// <summary>
        /// 首頁圖示
        /// </summary>
        [Description("首頁圖示")]
        public string FirstPageTitle { get; set; } = "首頁";

        /// <summary>
        /// 下一頁圖示
        /// </summary>
        [Description("下一頁圖示")]
        public string NextPageTitle { get; set; } = "下一頁";

        /// <summary>
        /// 上一頁圖示
        /// </summary>
        [Description("上一頁圖示")]
        public string PrevPageTitle { get; set; } = "上一頁";

        /// <summary>
        /// 尾頁圖示
        /// </summary>
        [Description("尾頁圖示")]
        public string LastPageTitle { get; set; } = "尾頁";

        /// <summary>
        /// 跳轉頁碼文本框寬度
        /// </summary>
        [Description("跳轉頁碼文本框寬度")]
        public int TextBoxPageWidth { get; set; } = 30;

        /// <summary>
        /// 跳轉頁碼文本框高度
        /// </summary>
        [Description("跳轉頁碼文本框高度")]
        public int TextBoxPageHeight { get; set; } = 17;

        /// <summary>
        /// 文本框頁碼字型
        /// </summary>
        [Description("文本框頁碼字型")]
        public Font TextBoxPageFont { get; set; } = new System.Drawing.Font("宋體", 9F);

        /// <summary>
        /// 文本框頁碼顔色
        /// </summary>
        [Description("文本框頁碼顔色")]
        public Color TextBoxPageColor { get; set; } = Color.Black;

        #endregion

        public event EventHandler OnPageChanged;
/// <summary>
        /// 隻有頁碼、首頁、尾頁、上一頁、下一頁
        /// </summary>
        [Description("分頁模闆6")]
        public bool PageModel6 { get; set; } = false;

        /// <summary>
        /// 隻有頁碼、首頁、尾頁、上一頁、下一頁
        /// </summary>
        [Description("分頁模闆100")]
        public bool PageModel100 { get; set; } = false;

        /// <summary>
        /// 目前資料隻有一頁時設定的顯示内容,不設定則顯示預設内容
        /// </summary>
        [Description("設定隻有一頁時展示的内容")]
        public string FirstPageMessage { get; set; } = "";

        /// <summary>
        /// 添加動态點選頁碼控件
        /// </summary>
        /// <param name="str">展示的内容</param>
        /// <param name="i">控件索引</param>
        /// <param name="fx">初始控件x軸距離</param>
        /// <param name="label1">控件</param>
        /// <param name="tag">臨時頁碼資料</param>
        public void AddControl(string str, int i, int fx, ref Label label1, string tag = "", bool isEnable = true)
        {
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(fx, YLocation);
            label1.Name = "PageLable" + i;
            label1.Size = new System.Drawing.Size(PageWidth, PageHeight);
            label1.TabIndex = i;
            label1.Text = str;
            label1.Tag = tag;
            label1.Enabled = isEnable;
            if (str == this.PageIndex.ToString())
            {
                label1.Font = CurrentNumFont;
                label1.ForeColor = CurrentNumColor;
            }
            else
            {
                label1.Font = NumFont;
                label1.ForeColor = NumColor;
                label1.Cursor = Cursors.Hand;
                label1.Click += new EventHandler(PageNumControl1_OnPageChanged);
            }
            this.panel1.Controls.Add(label1);
        }

        /// <summary>
        /// 添加動态展示控件
        /// </summary>
        /// <param name="str"></param>
        /// <param name="i"></param>
        /// <param name="fx"></param>
        /// <param name="label1"></param>
        /// <param name="tag"></param>
        public void AddShowControl(string str, int i, int fx, ref Label label1, string tag = "")
        {
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(fx, YLocation);
            label1.Name = "PageLable" + i;
            label1.Size = new System.Drawing.Size(PageWidth, PageHeight);
            label1.TabIndex = i;
            label1.Text = str;
            label1.Tag = tag;
            label1.Font = NumFont;
            label1.ForeColor = NumColor;
            this.panel1.Controls.Add(label1);
        }

        public void AddTextControl(string str, int i, int fx, ref TextBox txtBox, string tag = "")
        {
            txtBox.Location = new System.Drawing.Point(fx, YLocation);
            txtBox.Multiline = true;
            txtBox.Name = "txtBox" + i;
            txtBox.Text = str;
            txtBox.Size = new System.Drawing.Size(TextBoxPageWidth, TextBoxPageHeight);
            txtBox.TabIndex = i;
            txtBox.Tag = tag;
            txtBox.Font = TextBoxPageFont;
            txtBox.TextAlign = HorizontalAlignment.Center;
            txtBox.ForeColor = TextBoxPageColor;
            this.panel1.Controls.Add(txtBox);
        }

  /// <summary>
        /// 分頁模闆6
        /// </summary>
        public void GetPageModel6()
        {
            int totalCount = this.RecordCount;
            if (this.PageIndex <= 0)
            {
                this.PageIndex = 1;
            }
            int start = (this.PageIndex - 5 + this.PageOffset);
            if (start < 1)
            {
                start = 1;
            }
            int end = (start + this.PageBarNum - 1);
            if (end > this.PageCount)
            {
                start += this.PageCount - end;
                if (start <= 0)
                {
                    start = 1;
                }
                end = this.PageCount;
            }
            if (this.PageCount > 1)
            {
                StringBuilder outPut = new StringBuilder();
                if (this.EndIndex > totalCount)
                {
                    outPut.AppendFormat("第{0}-{1}條,", this.Startindex, totalCount);
                }
                else
                {
                    outPut.AppendFormat("第{0}-{1}條,", this.Startindex, this.EndIndex);
                }
                outPut.AppendFormat("每頁{1}條,共{0}條", totalCount, this.PageSize);
                Label lb1 = new Label();
                AddShowControl(outPut.ToString(), 1000000001, FirstXLocation, ref lb1, outPut.ToString());
                Label lb3 = new Label();
                if (this.PageIndex > 1)
                {
                    Label lb2 = new Label();
                    AddControl(FirstPageTitle, 1000000002, lb1.Width + lb1.Location.X + fwd, ref lb2, "首頁");
                    AddControl(PrevPageTitle, 1000000003, lb2.Width + lb2.Location.X + fwd, ref lb3, "上一頁");
                }
                else
                {
                    lb3 = lb1;
                }
                Label lb5 = new Label();
                int b = 0;
                for (int i = start; i <= end; i++)
                {
                    b++;
                    if (b > 1)
                    {
                        lb3 = lb5;
                        lb5 = new Label();
                    }
                    AddControl(i.ToString(), i + 1000, lb3.Width + lb3.Location.X + fwd, ref lb5, i.ToString());
                }
                Label lb6 = new Label();
                if (this.PageIndex != this.pageCount)
                {
                    Label lb4 = new Label();
                    AddControl(NextPageTitle, 1000000004, lb5.Width + lb5.Location.X + fwd, ref lb4, "下一頁");
                    AddControl(LastPageTitle, 1000000005, lb4.Width + lb4.Location.X + fwd, ref lb6, "尾頁");
                }
                else
                {
                    lb6 = lb5;
                }
                Label lb7 = new Label();
                if (!string.IsNullOrEmpty(LastShowText))
                {
                    AddShowControl(LastShowText, 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, LastShowText);
                }
                else
                {
                    AddShowControl("第" + this.PageIndex + "頁/共" + this.PageCount + "頁", 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, "第" + this.PageIndex + "頁/共" + this.PageCount + "頁");
                }

                TextBox tx = new TextBox();
                AddTextControl(this.PageIndex.ToString(), 200, lb7.Width + lb7.Location.X + fwd, ref tx, "");

                Label lb8 = new Label();
                AddControl("跳轉", 1000000009, tx.Width + tx.Location.X + fwd, ref lb8, "跳轉");
            }
            else
            {
                Label lb = new Label();
                StringBuilder outPut = new StringBuilder();
                if (string.IsNullOrEmpty(this.FirstPageMessage))
                {
                    outPut.AppendFormat("第{0}-{1}條,", this.Startindex, this.RecordCount);
                    outPut.AppendFormat("每頁{0}條,共{1}條", this.PageSize, this.RecordCount);
                }
                else
                {
                    outPut.AppendFormat(this.FirstPageMessage);
                }
                AddShowControl(outPut.ToString(), 1002, fwd, ref lb, outPut.ToString());
            }
        }

        /// <summary>
        /// 分頁模闆100
        /// </summary>
        public void GetPageModel100()
        {
            int totalCount = this.RecordCount;
            if (this.PageIndex <= 0)
            {
                this.PageIndex = 1;
            }
            int start = (this.PageIndex - 5 + this.PageOffset);
            if (start < 1)
            {
                start = 1;
            }
            int end = (start + this.PageBarNum - 1);
            if (end > this.PageCount)
            {
                start += this.PageCount - end;
                if (start <= 0)
                {
                    start = 1;
                }
                end = this.PageCount;
            }
            StringBuilder outPut = new StringBuilder();
            if (this.EndIndex > totalCount)
            {
                outPut.AppendFormat("第{0}-{1}條,", this.Startindex, totalCount);
            }
            else
            {
                outPut.AppendFormat("第{0}-{1}條,", this.Startindex, this.EndIndex);
            }
            outPut.AppendFormat("每頁{1}條,共{0}條", totalCount, this.PageSize);
            Label lb1 = new Label();
            AddShowControl(outPut.ToString(), 1000000001, FirstXLocation, ref lb1, outPut.ToString());
            Label lb3 = new Label();
            bool isenable = true;
            if (this.PageIndex <= 1)
            {
                isenable = false;
            }
            Label lb2 = new Label();
            AddControl(FirstPageTitle, 1000000002, lb1.Width + lb1.Location.X + fwd, ref lb2, "首頁", isenable);
            AddControl(PrevPageTitle, 1000000003, lb2.Width + lb2.Location.X + fwd, ref lb3, "上一頁", isenable);
            Label lb6 = new Label();
            isenable = true;
            if (this.PageIndex == this.pageCount)
            {
                isenable = false;
            }
            Label lb4 = new Label();
            AddControl(NextPageTitle, 1000000004, lb3.Width + lb3.Location.X + fwd, ref lb4, "下一頁", isenable);
            AddControl(LastPageTitle, 1000000005, lb4.Width + lb4.Location.X + fwd, ref lb6, "尾頁", isenable);
            Label lb7 = new Label();
            if (!string.IsNullOrEmpty(LastShowText))
            {
                AddShowControl(LastShowText, 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, LastShowText);
            }
            else
            {
                AddShowControl("第" + this.PageIndex + "頁/共" + this.PageCount + "頁", 1000000006, lb6.Width + lb6.Location.X + fwd, ref lb7, "第" + this.PageIndex + "頁/共" + this.PageCount + "頁");
            }

            TextBox tx = new TextBox();
            AddTextControl(this.PageIndex.ToString(), 200, lb7.Width + lb7.Location.X + fwd, ref tx, "");

            Label lb8 = new Label();
            AddControl("跳轉", 1000000009, tx.Width + tx.Location.X + fwd, ref lb8, "跳轉");

        }

 public void PageNumControl_Load(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();
            this.PageIndex = 1;
            if (OnPageChanged != null)
            {
                OnPageChanged(this, null);//目前分頁數字改變時,觸發委托事件
            }
            if (this.PageModel6)
            {
                GetPageModel6();
            }
            else if (this.PageModel100)
            {
                GetPageModel100();
            }
        }

        public void PageNumControl1_OnPageChanged(object sender, EventArgs e)
        {
            SetPageText();
            this.panel1.Controls.Clear();
            Label btn = sender as Label;
            if (btn.Tag.ToString().Trim() == "首頁")
            {
                this.PageIndex = 1;
            }
            else if (btn.Tag.ToString().Trim() == "上一頁")
            {
                this.PageIndex -= 1;
            }
            else if (btn.Tag.ToString().Trim() == "下一頁")
            {
                this.PageIndex += 1;
            }
            else if (btn.Tag.ToString().Trim() == "尾頁")
            {
                this.PageIndex = this.pageCount;
            }
            else if (btn.Tag.ToString().Trim() == "跳轉")
            {
                // SetPageText();
            }
            else
            {
                this.PageIndex = Convert.ToInt32(btn.Text);
            }
            if (OnPageChanged != null)
            {
                OnPageChanged(this, null);//目前分頁數字改變時,觸發委托事件
            }
           if (this.PageModel6)
            {
                GetPageModel6();
            }
            else if (this.PageModel100)
            {
                GetPageModel100();
            }
        }

        public void SetPageText()
        {
            foreach (Control c in this.panel1.Controls)
            {
                if (c is TextBox)
                {
                    TextBox tbox = (TextBox)c;
                    if (tbox.Name == "txtBox200")
                    {
                        int pagenum = 1;
                        int.TryParse(tbox.Text, out pagenum);
                        if (pagenum == 0)
                        {
                            pagenum = 1;
                        }
                        this.PageIndex = pagenum;
                    }
                }
            }
        }

        /// <summary>
        /// 更像清單資料
        /// </summary>
        public void UpdateShowData()
        {
            if (OnPageChanged != null)
            {
                OnPageChanged(this, null);//目前分頁數字改變時,觸發委托事件
            }
            PageNumControl_Load(null, null);
        }      

然後下面是該使用者控件使用方式,由于頁面的頁碼條和按鈕都是動态生成的,是以如果有需要特别設定的樣式,可以在代碼中定義對應的屬性,在引用的時候進行設定

public FrmHome()
        {
            InitializeComponent();
            pageNumControl1.OnPageChanged += new EventHandler(pageNumControl1_OnPageChanged);
        }

        public void BindData()
        {
            PageModelTool pl= ClockBll.GetTable(pageNumControl1.PageSize, pageNumControl1.PageIndex);//擷取資料清單(該方法自己根據需要封裝即可),将擷取的table資料和總數等分頁資料,封裝到PageModelTool類中(自己定義封裝屬性,想要啥封裝啥)
            gvClockList.AutoGenerateColumns = false;
            gvClockList.DataSource = pl.table;
            pageNumControl1.RecordCount = pl.Number;//資料總數
        }

        private void pageNumControl1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

       說明:FrmHome是窗體界面,gvClockList是DataGridView控件,pageNumControl1是使用者分頁控件      

下面是分頁使用者控件設定的屬性,可以直接複制到設計窗體背景代碼中

this.pageNumControl1.CurrentNumColor = System.Drawing.Color.Red;
            this.pageNumControl1.CurrentNumFont = new System.Drawing.Font("宋體", 12F);
            this.pageNumControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pageNumControl1.FirstPageMessage = "";
            this.pageNumControl1.FirstPageTitle = "首頁";
            this.pageNumControl1.FirstXLocation = 10;
            this.pageNumControl1.ForeColor = System.Drawing.Color.Red;
            this.pageNumControl1.fwd = 10;
            this.pageNumControl1.LastPageTitle = "尾頁";
            this.pageNumControl1.LastShowText = null;
            this.pageNumControl1.Location = new System.Drawing.Point(0, 0);
            this.pageNumControl1.Margin = new System.Windows.Forms.Padding(5);
            this.pageNumControl1.Name = "pageNumControl1";
            this.pageNumControl1.NextPageTitle = "下一頁";
            this.pageNumControl1.NumColor = System.Drawing.Color.Black;
            this.pageNumControl1.NumFont = new System.Drawing.Font("宋體", 12F);
            this.pageNumControl1.PageBarNum = 10;
            this.pageNumControl1.PageHeight = 12;this.pageNumControl1.PageModel100 = false;this.pageNumControl1.PageModel6 = true;// 這個屬性表示你使用該分頁模闆來顯示分頁頁碼條
            this.pageNumControl1.PageOffset = 0;
            this.pageNumControl1.PageSize = 24;
            this.pageNumControl1.PageWidth = 179;
            this.pageNumControl1.PrevPageTitle = "上一頁";
            this.pageNumControl1.RecordCount = 0;
            this.pageNumControl1.Size = new System.Drawing.Size(1130, 53);
            this.pageNumControl1.TabIndex = 0;
            this.pageNumControl1.TextBoxPageColor = System.Drawing.Color.Black;
            this.pageNumControl1.TextBoxPageFont = new System.Drawing.Font("宋體", 9F);
            this.pageNumControl1.TextBoxPageHeight = 17;
            this.pageNumControl1.TextBoxPageWidth = 48;
            this.pageNumControl1.YLocation = 20;
            this.pageNumControl1.OnPageChanged += new System.EventHandler(this.pageNumControl1_OnPageChanged);      

下面就是最終的效果啦...

分享一個自己寫的winform版的分頁使用者控件
分享一個自己寫的winform版的分頁使用者控件
分享一個自己寫的winform版的分頁使用者控件

 有需要的朋友希望可以幫到你,也可以在這個基礎上封裝自己需要的分頁樣式,我這邊就隻是封裝了一個自己用的基本樣式了

總有一個讓你努力的理由,相信自己,加油!