运行效果:
控件结构图:
实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | using System; using System.ComponentModel; using System.Drawing; using System.Text.RegularExpressions; using System.Windows.Forms; namespace KK.Pagers { public partial class Pagers : UserControl { #region 定义、构造 private int _pageIndex = 1; /// <summary> /// 当前页数 /// </summary> public int PageIndex { get { return _pageIndex; } set { _pageIndex = value; } } private int _pageSize; /// <summary> /// 每一页显示的数据量 /// </summary> public int PageSize { get { return _pageSize; } set { _pageSize = value; } } private int _pageCount; /// <summary> /// 总数据数 /// </summary> public int PageCount { get { if (PageSize != 0) { _pageCount = GetPageCount(); } return _pageCount; } } private int _totalCount = 20; /// <summary> /// 总数 /// </summary> public int TotalCount { get { return _totalCount; } set { _totalCount = value; } } /// <summary> /// 首页背景图 /// </summary> public Image FirstBackgroundImage { get { return pboxFirst.Image; } set { pboxFirst.Image = value; } } /// <summary> /// 上一页背景图 /// </summary> public Image LastBackgroundImage { get { return this .pboxUp.Image; } set { pboxUp.Image = value; } } /// <summary> /// 下一个背景图 /// </summary> public Image NextBackgroundImage { get { return this .pboxNext.Image; } set { pboxNext.Image = value; } } /// <summary> /// 尾页背景图 /// </summary> public Image EndBackgroundImage { get { return this .pboxEnd.Image; } set { pboxEnd.Image = value; } } /// <summary> /// 构造 /// </summary> public Pagers() { InitializeComponent(); this .Load += Pagers_Load; } #endregion #region event void Pagers_Load( object sender, EventArgs e) { if (PageIndex <= 0) PageIndex = 1; } [Browsable( true ), Description( "分页改变事件" ), Category( "操作" )] public event EventHandler OnPageChanged; //首页 private void pboxPageFirst_Click( object sender, EventArgs e) { PageIndex = 1; DrawControl( true ); } //上一页 private void pboxPre_Click( object sender, EventArgs e) { if (PageIndex > 1) { PageIndex--; } DrawControl( true ); } //下一页 private void pboxNext_Click( object sender, EventArgs e) { if (PageIndex < PageCount) { PageIndex++; } DrawControl( true ); } //尾页 private void pboxLast_Click( object sender, EventArgs e) { PageIndex = PageCount; DrawControl( true ); } private void ctboxCurrentPageIndex_KeyDown( object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { int page = -1; if ( int .TryParse(ctboxCurrentPageIndex.Text, out page) && int .Parse(ctboxCurrentPageIndex.Text) > 0 && int .Parse(ctboxCurrentPageIndex.Text) <= PageCount && PageIndex != int .Parse(ctboxCurrentPageIndex.Text)) { PageIndex = int .Parse(ctboxCurrentPageIndex.Text); DrawControl( true ); } else { ctboxCurrentPageIndex.Text = PageIndex.ToString(); ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; } } } private void ctboxCurrentPageIndex_TextChanged( object sender, EventArgs e) { Regex reg = new Regex( "^[0-9]*$" ); if (!reg.IsMatch(ctboxCurrentPageIndex.Text)) { ctboxCurrentPageIndex.Text = PageIndex.ToString(); ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; } if (PageCount.Equals(0)) { DisplayImage( this .pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1); DisplayImage( this .pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1); DisplayImage( this .pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1); DisplayImage( this .pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1); return ; } //首页且不止一页数据 if (PageIndex.Equals(1) && !PageIndex.Equals(PageCount)) { DisplayImage( this .pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1); DisplayImage( this .pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1); DisplayImage( this .pboxNext, global::KK.Pagers.Properties.Resources.Page_Next); DisplayImage( this .pboxEnd, global::KK.Pagers.Properties.Resources.Page_End); return ; } //只有一页数据 if (PageIndex.Equals(1) && PageIndex.Equals(PageCount)) { DisplayImage( this .pboxFirst, global::KK.Pagers.Properties.Resources.Page_First1); DisplayImage( this .pboxUp, global::KK.Pagers.Properties.Resources.Page_Last1); DisplayImage( this .pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1); DisplayImage( this .pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1); return ; } //尾页 if (PageIndex == PageCount) { DisplayImage( this .pboxFirst, global::KK.Pagers.Properties.Resources.Page_First); DisplayImage( this .pboxUp, global::KK.Pagers.Properties.Resources.Page_Last); DisplayImage( this .pboxNext, global::KK.Pagers.Properties.Resources.Page_Next1); DisplayImage( this .pboxEnd, global::KK.Pagers.Properties.Resources.Page_End1); return ; } //大于第一页 if (PageIndex > 1 && PageIndex < PageCount) { DisplayImage( this .pboxFirst, global::KK.Pagers.Properties.Resources.Page_First); DisplayImage( this .pboxUp, global::KK.Pagers.Properties.Resources.Page_Last); DisplayImage( this .pboxNext, global::KK.Pagers.Properties.Resources.Page_Next); DisplayImage( this .pboxEnd, global::KK.Pagers.Properties.Resources.Page_End); return ; } } #endregion #region private method /// <summary> /// 设置控件是否显示 /// </summary> /// <param name="pb">PictureBox pb</param> /// <param name="img">图片</param> private void DisplayImage(PictureBox pb, Image img) { if (img != null ) pb.Image = img; } /// <summary> /// 获取页数 /// </summary> /// <returns>页数</returns> private int GetPageCount() { if (PageSize == 0) { return 0; } int pageCount = TotalCount / PageSize; if (TotalCount % PageSize == 0) { pageCount = TotalCount / PageSize; } else { pageCount = TotalCount / PageSize + 1; } return pageCount; } /// <summary> /// 显示页数 /// </summary> /// <param name="count">页数</param> public void DrawControl( int count) { TotalCount = count; this .lblPageCountShow.Text = string .Format( "共{0}页" , PageCount); DrawControl( false ); } /// <summary> /// 分页控件可用性设置 /// </summary> /// <param name="callEvent">callEvent</param> private void DrawControl( bool callEvent) { this .ctboxCurrentPageIndex.Text = PageIndex.ToString(); this .ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; if (callEvent && OnPageChanged != null ) OnPageChanged( this , null ); SetFormCtrEnabled(); if (PageCount == 0) { this .ctboxCurrentPageIndex.Text = "1" ; this .ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; this .pboxFirst.Enabled = false ; this .pboxUp.Enabled = false ; this .pboxNext.Enabled = false ; this .pboxEnd.Enabled = false ; } if (PageCount == 1) { this .ctboxCurrentPageIndex.Text = "1" ; this .ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; this .pboxFirst.Enabled = false ; this .pboxUp.Enabled = false ; this .pboxNext.Enabled = false ; this .pboxEnd.Enabled = false ; } else if (PageIndex == 1) { this .ctboxCurrentPageIndex.Text = "1" ; this .ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; this .pboxFirst.Enabled = false ; this .pboxUp.Enabled = false ; } else if (PageIndex == PageCount) { this .ctboxCurrentPageIndex.Text = PageCount.ToString(); this .ctboxCurrentPageIndex.SelectionStart = ctboxCurrentPageIndex.TextLength; this .pboxNext.Enabled = false ; this .pboxEnd.Enabled = false ; } } private void SetFormCtrEnabled() { this .pboxFirst.Enabled = true ; this .pboxNext.Enabled = true ; this .pboxUp.Enabled = true ; this .pboxEnd.Enabled = true ; } #endregion private void Pbox_EnabledChanged( object sender, EventArgs e) { PictureBox pbox = sender as PictureBox; bool isEnable = pbox.Enabled; Image pageimage; switch (pbox.Name) { case "pboxFirst" : pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_First : global::KK.Pagers.Properties.Resources.Page_First1; break ; case "pboxNext" : pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Next : global::KK.Pagers.Properties.Resources.Page_Next1; break ; case "pboxUp" : pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_Last : global::KK.Pagers.Properties.Resources.Page_Last1; break ; default : pageimage = isEnable ? global::KK.Pagers.Properties.Resources.Page_End : global::KK.Pagers.Properties.Resources.Page_End1; break ; } DisplayImage(pbox, pageimage); } private void Pagers_Load_1( object sender, EventArgs e) { if (GetPageCount() > 0) ctboxCurrentPageIndex.Text = "1" ; else ctboxCurrentPageIndex.Text = "0" ; } } } |
调用方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Demo { public partial class Form2 : Form { List<StudentInfo> list = new List<StudentInfo>(); public Form2() { InitializeComponent(); this .dgvList.AutoGenerateColumns = false ; this .pagingList.PageIndex = 1; for ( int i = 1; i <= 33; i++) { StudentInfo stu = new StudentInfo(); stu.Name = "学生" + i; stu.Age = i; list.Add(stu); } } private void Form2_Load( object sender, EventArgs e) { GetPageData(); } private void GetPageData() { List<StudentInfo> dataSource = list .Skip(( this .pagingList.PageIndex - 1) * this .pagingList.PageSize) .Take( this .pagingList.PageSize).ToList(); var count = Math.Ceiling( double .Parse(Convert.ToString(list.Count / dataSource.Count))); this .dgvList.DataSource = dataSource; this .pagingList.TotalCount = list.Count; //调用page的公用方法 this .pagingList.DrawControl(list.Count); } private void pagingList_OnPageChanged( object sender, EventArgs e) { GetPageData(); } } public class StudentInfo { public string Name { get ; set ; } public int Age { get ; set ; } } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本