winform自定义分页控件

1、控件代码:

public partial class PagerControl : UserControl {
        #region 构造函数

        public PagerControl() {
            InitializeComponent();
        }

        #endregion

        #region 分页字段和属性

        private int pageIndex = 1;
        /// <summary>
        /// 当前页面
        /// </summary>
        public virtual int PageIndex {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        private int pageSize = 1;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public virtual int PageSize {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int recordCount = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public virtual int RecordCount {
            get { return recordCount; }
            set { recordCount = value; }
        }

        private int pageCount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        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;
        }
        #endregion

        public event EventHandler OnPageChanged;

        /// <summary>
        /// 外部调用
        /// </summary>
        public void DrawControl(int count) {
            recordCount = count;
            DrawControl(false);
        }
        /// <summary>
        /// 页面控件呈现
        /// </summary>
        private void DrawControl(bool callEvent) {
            this.GoItem.Text = "跳转";
            this.CurrentItem.Text = PageIndex.ToString();
            this.TolCountItem.Text = RecordCount.ToString();
            this.PageSizeItem.Text = PageSize.ToString();

            if (callEvent && OnPageChanged != null) {
                OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
            }
            SetFormCtrEnabled();
            if (PageCount == 1)//有且仅有一页
            {
                this.MoveFirstItem.Enabled = false;
                this.MovePreviousItem.Enabled = false;
                this.MoveNextItem.Enabled = false;
                this.MoveLastItem.Enabled = false;
            }
            else if (PageIndex == 1)//第一页
            {
                this.MoveFirstItem.Enabled = false;
                this.MovePreviousItem.Enabled = false;
            }
            else if (PageIndex == PageCount)//最后一页
            {
                this.MoveNextItem.Enabled = false;
                this.MoveLastItem.Enabled = false;
            }
        }

        /// <summary>
        /// 设置控件可用性
        /// </summary>
        private void SetFormCtrEnabled() {
            this.CurrentItem.Enabled = true;
            this.MoveFirstItem.Enabled = true;
            this.MovePreviousItem.Enabled = true;
            this.MoveNextItem.Enabled = true;
            this.MoveLastItem.Enabled = true;
            this.GoItem.Enabled = true;
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveFirstItem_Click(object sender, EventArgs e) {
            PageIndex = 1;
            DrawControl(true);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MovePreviousItem_Click(object sender, EventArgs e) {
            PageIndex = Math.Max(1, PageIndex - 1);
            DrawControl(true);
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveNextItem_Click(object sender, EventArgs e) {
            PageIndex = Math.Min(PageCount, PageIndex + 1);
            DrawControl(true);
        }

        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MoveLastItem_Click(object sender, EventArgs e) {
            PageIndex = PageCount;
            DrawControl(true);
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GoItem_Click(object sender, EventArgs e) {
            int num = 0;
            if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > 0) {
                PageIndex = num;
                DrawControl(true);
            }
        }

        /// <summary>
        /// 跳转页数限制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentItem_TextChanged(object sender, EventArgs e) {
            int num = 0;
            if (int.TryParse(this.CurrentItem.Text.Trim(), out num) && num > 0) {
                if (num > PageCount) {
                    this.CurrentItem.Text = PageCount.ToString();
                }
            }
        }

        /// <summary>
        /// 页数Enter键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentItem_KeyPress(object sender, KeyPressEventArgs e) {
            GoItem_Click(null, null);
        }

        private bool isTextChanged = false;

        /// <summary>
        /// 页数改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageSizeItem_TextChanged(object sender, EventArgs e) {
            int num = 0;
            if (!int.TryParse(this.PageSizeItem.Text.Trim(), out num) || num <= 0) {
                num = 20;
                this.PageSizeItem.Text = "20";
            }
            else {
                isTextChanged = true;
            }
            pageSize = num;
        }

        /// <summary>
        /// 页数Enter事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageSizeItem_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == System.Convert.ToChar(13)) {
                if (isTextChanged) {
                    isTextChanged = false;
                    MoveFirstItem_Click(null, null);
                }
            }
        }
    } 

2、调用代码:

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectForm_Load(object sender, EventArgs e) {
            LoadData();
            pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);
        }

        /// <summary>
        /// 分页控件改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e) {
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="pagesize"></param>
        private void LoadData() {
            List<Template> templateList = new List<Template>();
            HttpHelper http = new HttpHelper();
            string url = string.Format("https://www.baidu.com?page={0}&pagecount={1}", this.pagerControl1.PageIndex, this.pagerControl1.PageSize);
            string result = http.CollectHtml(url, "", HttpHelper.Method.GET, HttpHelper.HtmlEncoding.UTF8, HttpHelper.HtmlEncoding.UTF8);
            var resultData = JsonConvert.DeserializeObject<JObject>(result);
                //分页信息
                if (resultData["flipinfo"] != null) {
                    Page page = JsonConvert.DeserializeObject<Page>(resultData["flipinfo"].ToString());
                    if (page != null) {
                        pagerControl1.DrawControl(page.total);
                    }
                }
        }

  以上是实际项目中使用的代码,供大家参考。

原文地址:https://www.cnblogs.com/len0031/p/11273462.html

时间: 2024-10-10 11:04:46

winform自定义分页控件的相关文章

winform 自定义分页控件 及DataGridview数据绑定

分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; usin

Winform自定义分页控件的实现

实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { private int record = 0; /// <summary> /// 总记录数 /// </summary> public int Record { get { return record; } set { record = value; InitPageInfo(); } } pr

WINFORM 自定义开关按钮控件-

本文章转载:http://www.cnblogs.com/feiyangqingyun/archive/2013/06/15/3137597.html OK,大工告成,上图演示效果. 源码下载:http://files.cnblogs.com/feiyangqingyun/myButtonCheckTest.zip WINFORM 自定义开关按钮控件-,布布扣,bubuko.com

Mvc自定义分页控件

MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候,完全显示出来: 2>当分页超过10页,效果图如下所示: 再来看看生成的分页代码,竟然和楼主一样纯洁!!! 3>支持自定义路由.参数.页码html格式.首末页是否显示.上下页是否显示.URL分页参数传递功能. 使用方法: 1.引用 YYP.PagerHtml.dll 2.页面使用 @using YY

WPF管理系统自定义分页控件 - WPF特工队内部资料

原文:WPF管理系统自定义分页控件 - WPF特工队内部资料 最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明细逻辑处理: 3.页码总数小于7时显示默认显示,大于7时切换复杂显示: 4.页码数.索引.总条数计算等: 先来一张效果图: 啥也不说了直接上代码 MISPager.xaml部分 <ResourceDictionary xmlns="

自定义分页控件PageList

1 using System; 2 namespace Commons.PageList 3 { 4 interface IPageList 5 { 6 int PageIndex { get; } 7 int PageSize { get; } 8 int TotalCount { get; } 9 int TotalPage { get; } 10 } 11 } 1 using System.Collections; 2 using System.Collections.Generic; 3

freemaker自定义分页控件实现

<link href="${res}/css/pages-jhdb.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="${res}/js/laypage/laypage.js" ></script> #pages-jhdb{padding-left:5px;} /*!

C# winform自定义Label控件使其能设置行距

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.ComponentModel; namespace WindowsFormsApplication10 { public partial class LabelTx : System.Windows.Forms.Label { int lineDistanc

easyui-datagrid自定义分页控件样式

var p = $('#tbTaskList').datagrid('getPager'); $(p).pagination({ pageSize: 20,//每页显示的记录条数,默认为10 pageList: [20, 50, 100],//可以设置每页记录条数的列表 beforePageText: '第',//页数文本框前显示的汉字 afterPageText: '页 共 {pages} 页', displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记