winform自定义分页控件

Posted len0031

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
                    
                
        

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

以上是关于winform自定义分页控件的主要内容,如果未能解决你的问题,请参考以下文章

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

C# winform 类似于如下图显示用啥控件绑定并实现分页

c#winform 分页控件怎么添加

[转]Oracle分页之二:自定义web分页控件的封装

Winform 通用分页控件实战篇(提供源码下载)

Winform 通用分页控件实战篇(提供源码下载)