开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 4097|回复: 2

winform自定义分页控件

[复制链接]

0

精华

599

贡献

10

赞扬

赞助者组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
14
软币
141
在线时间
12 小时
注册时间
2016-1-7
发表于 2019-7-18 11:24:09 | 显示全部楼层 |阅读模式
本帖最后由 pjl291064201 于 2019-7-18 11:27 编辑

图样,将就着用

样式

样式

添加自定义控件pageControl类。
拉5个button,一个combox,一个textbox,一个label控件。样式自己调整一下,我这里用的是dev的button和图标。
后面搜索重置之类的按钮是我根据自己的项目添加的,排版好看一点,看自己有没有必要添加。
下面是源码
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WH_UI_BASE.Control
{
    public partial class PageControl : UserControl
    {
        #region 分页初始化

        private int record = 0;

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Record
        {
            get { return record; }
            set
            {
                record = value;
                InitPageInfo();
            }
        }

        private int pageSize = 20;

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }


        /// <summary>
        /// MYSQL limit开始条数
        /// </summary>
        public int Start
        {
            get { return (currentPage - 1) * pageSize; }
        }


        private int currentPage = 1;

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        public int pageNum = 0;

        /// <summary>
        /// 总页码
        /// </summary>
        public int PageNum
        {
            get
            {
                if (Record == 0)
                {
                    pageNum = 0;
                }
                else
                {
                    if (Record % PageSize > 0)
                    {
                        pageNum = Record / PageSize + 1;
                    }
                    else
                    {
                        pageNum = Record / PageSize;
                    }
                }
                return pageNum;
            }

        }

        //定义委托
        public delegate void BindHandle(object sender, EventArgs e);

        /// <summary>
        /// 绑定数据源事件
        /// </summary>
        public event BindHandle BindSource;

        /// <summary>
        /// 跳转第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_first_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    MessageBox.Show("当前已经是首页");
                    //new Toast("当前已经是首页",2000);
                    return;
                }
                else
                {
                    CurrentPage = 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        /// <summary>
        /// 跳转上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_previous_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == 1)
                {
                    MessageBox.Show("当前已经是首页");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage - 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        /// <summary>
        /// 跳转下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_next_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    MessageBox.Show("当前已经是末页");
                    return;
                }
                else
                {
                    CurrentPage = CurrentPage + 1;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        /// <summary>
        /// 跳转最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_last_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (CurrentPage == PageNum)
                {
                    MessageBox.Show("当前已经是末页");
                    return;
                }
                else
                {
                    CurrentPage = PageNum;
                    if (BindSource != null)
                    {
                        BindSource(sender, e);
                        InitPageInfo();
                    }
                }
            }
        }

        /// <summary>
        /// 跳转任意一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_go_Click(object sender, EventArgs e)
        {
            if (Record > 0)
            {
                if (!string.IsNullOrEmpty(this.tex_pagenum.Text) && !Regex.IsMatch(this.tex_pagenum.Text, @"^[\d]*$"))
                {
                    MessageBox.Show("请正确填写页码!");
                    return;
                }
                int page = Convert.ToInt32(this.tex_pagenum.Text);
                if (page == 0)
                {
                    page = 1;
                }
                if (page > PageNum)
                {
                    page = PageNum;
                }

                CurrentPage = page;
                if (BindSource != null)
                {
                    BindSource(sender, e);
                    InitPageInfo();
                }
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitPageInfo()
        {
            if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
            {
                CurrentPage = 1;
            }
            lbl_info.Text = string.Format("共 {0} 条记录  共 {1} 页  当前第 {2} 页", Record, PageNum, CurrentPage);
            tex_pagenum.Text = CurrentPage.ToString();

            if (CurrentPage <= 1)
            {
                this.btn_first.Enabled = false;
                this.btn_previous.Enabled = false;
            }
            else
            {
                this.btn_first.Enabled = true;
                this.btn_previous.Enabled = true;
            }

            if (CurrentPage >= PageNum)
            {
                this.btn_next.Enabled = false;
                this.btn_last.Enabled = false;
            }
            else
            {
                this.btn_next.Enabled = true;
                this.btn_last.Enabled = true;
            }

        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageControl_Load(object sender, EventArgs e)
        {
            if (BindSource != null)
            {
                BindSource(sender, e);
                InitPageInfo();
            }
        }

        [Description("搜索按钮点击事件")]
        public event EventHandler PSearchClick;
        [Description("重置按钮点击事件")]
        public event EventHandler PResetClick;
        [Description("新增按钮点击事件")]
        public event EventHandler PAddClick;
        [Description("删除按钮点击事件")]
        public event EventHandler PDeleteClick;
        [Description("模板按钮点击事件")]
        public event EventHandler PModalClick;
        [Description("导入按钮点击事件")]
        public event EventHandler PImportClick;
        [Description("导出按钮点击事件")]
        public event EventHandler PExportClick;

        private void Btn_search_Click(object sender, EventArgs e)
        {
            if (PSearchClick != null)
            {
                PSearchClick(sender, e);
            }
        }

        private void Btn_reset_Click(object sender, EventArgs e)
        {
            if (PResetClick != null)
            {
                PResetClick(sender, e);
            }
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            if (PAddClick != null)
            {
                PAddClick(sender, e);
            }
        }

        private void Btn_delete_Click(object sender, EventArgs e)
        {
            if (PDeleteClick != null)
            {
                PDeleteClick(sender, e);
            }
        }

        private void Btn_modal_Click(object sender, EventArgs e)
        {
            if (PModalClick != null)
            {
                PModalClick(sender, e);
            }
        }

        private void Btn_import_Click(object sender, EventArgs e)
        {
            if (PImportClick != null)
            {
                PImportClick(sender, e);
            }
        }

        private void Btn_export_Click(object sender, EventArgs e)
        {
            if (PExportClick != null)
            {
                PExportClick(sender, e);
            }
        }

        #endregion

        public PageControl()
        {
            InitializeComponent();
        }



        #region 控件位置初始化


        private int posNumAdd = 0;
        private int posNumDelete = 0;
        private int posNumImport = 0;
        private int posNumExport = 0;
        private int posNumModal = 0;

        
        public int PosNumAdd
        {
            get => posNumAdd; set
            {
                posNumAdd = value;
                if (value >= 0 && value <= 7)
                {
                    btn_add.Visible = !(value == 0);
                    btn_add.Location = new Point(btn_search.Location.X - value * (btn_reset.Location.X- btn_search.Location.X), btn_search.Location.Y);
                }
            }
        }
        public int PosNumDelete
        {
            get => posNumDelete; set
            {
                posNumDelete = value;
                if (value >= 0 && value <= 7)
                {
                    btn_delete.Visible = !(value == 0);
                    btn_delete.Location = new Point(btn_search.Location.X - value * (btn_reset.Location.X - btn_search.Location.X), btn_search.Location.Y);
                }
            }
        }
        public int PosNumImport
        {
            get => posNumImport; set
            {
                posNumImport = value;
                if (value >= 0 && value <= 7)
                {
                    btn_import.Visible = !(value == 0);
                    btn_import.Location = new Point(btn_search.Location.X - value * (btn_reset.Location.X - btn_search.Location.X), btn_search.Location.Y);
                }
            }
        }
        public int PosNumExport
        {
            get => posNumExport; set
            {
                posNumExport = value;
                if (value >= 0 && value <= 7)
                {
                    btn_export.Visible = !(value == 0);
                    btn_export.Location = new Point(btn_search.Location.X - value * (btn_reset.Location.X - btn_search.Location.X), btn_search.Location.Y);
                }
            }
        }
        public int PosNumModal
        {
            get => posNumModal; set
            {
                posNumModal = value;
                if (value >= 0 && value <= 7)
                {
                    btn_modal.Visible = !(value == 0);
                    btn_modal.Location = new Point(btn_search.Location.X - value * (btn_reset.Location.X - btn_search.Location.X), btn_search.Location.Y);
                }
            }
        }

        #endregion

/// <summary>
/// 改变每页显示条数时重新初始化分页控件
/// </summary>
        private void Cbe_pagesize_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageSize = Convert.ToInt32(this.cbe_pagesize.Text);
            CurrentPage = 1;
            BindSource(sender, e);
            InitPageInfo();
        }
    }
}


下面是项目中引用
[C#] 纯文本查看 复制代码
/// <summary>
        /// 绑定数据-这里用的是MySQL
        /// </summary>
        private void Bind()
        {
            DAL.t_system_user user = new DAL.t_system_user();
            Model.System.t_system_user userInfo = new Model.System.t_system_user();
            if (this.lte_user_no.Text != null && this.lte_user_no.Text != "")
            {
                userInfo.User_no = this.lte_user_no.Text;
            }
            if (this.lte_user_name.Text != null && this.lte_user_name.Text != "")
            {
                userInfo.User_name = this.lte_user_name.Text;
            }
            int record = user.GET_USER_PAGE_RECORD(userInfo);
            DataSet ds = user.GET_USER_PAGE(userInfo, pageControl1.Start, pageControl1.PageSize);
            pageControl1.Record = record;
            this.gridControl1.DataSource = ds.Tables[0];
            //this.gridView1.PopulateColumns();
        }

        /// <summary>
        /// 监控数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageControl1_BindSource(object sender, EventArgs e)
        {
            Bind();
        }

回复

使用道具 举报

0

精华

0

贡献

82

赞扬

帖子
30
软币
320
在线时间
19 小时
注册时间
2014-4-9
发表于 2019-8-24 15:51:55 | 显示全部楼层
demo  源码可以分享吗?
回复

使用道具 举报

0

精华

599

贡献

10

赞扬

赞助者组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
14
软币
141
在线时间
12 小时
注册时间
2016-1-7
 楼主| 发表于 2020-6-18 11:06:03 | 显示全部楼层
这就是昵称 发表于 2019-8-24 15:51
demo  源码可以分享吗?

很久没上论坛了,源码demo还要的话你发邮件到yvling2008@163.com
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-12-22 15:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表