最近在开发一个 Winform 的项目,在项目中自己写了一个类似于 Web 端的分页管理器 。能力有限,也许还有可以优化的地方,欢迎大家指点。话不多说,奉上源码。
一。新建一个 winform 项目,在 form 页中准备控件如下:
ComboBox (1个) : comboBox_pageSize 用来存放每页允许显示的最大记录条数 ( 属性DropDownStyle : DropDownList) 。
PictureBox (4个) : 用来存放 四个翻页按钮。
TextBox (1个) : textBox_thisPage 用来存放当前页信息。
Label (若干个) : 用来存放其他文字 与 条数 信息。
二。代码实现
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace wcerp.baseSet.authority.personManage
{
public partial class List : Form
{
#region 分页插件
// 初始化分页工具栏
private void page()
{
int allNum = Convert.ToInt32(common.listCounts); // 当前页面全部记录条数
int totalNum = dataGridView1.RowCount; // 当前DGV记录条数
int pageSize = Convert.ToInt32(comboBox_pageSize.Text); // 每页显示条数
int thisPage = 0; // 当前页码
if (server.isEmptyS(textBox_thisPage.Text))
{
thisPage = 1;
}
else
{
thisPage = Convert.ToInt32(textBox_thisPage.Text);
}
// 计算最大页码
float pageNumFloat = (float)allNum / pageSize;
string pageNum = Math.Ceiling(pageNumFloat).ToString();
label_pageNum.Text = pageNum;
// 计算当前页起始记录条数 和 截至记录条数
int startNum = 1 + (thisPage -1) * pageSize;
label_startNum.Text = startNum.ToString();
int endNum = pageSize + (thisPage - 1) * pageSize;
if (endNum > allNum)
{
label_endNum.Text = allNum.ToString();
}
else
{
label_endNum.Text = endNum.ToString();
}
// 设置最大记录数
label_recordNum.Text = totalNum.ToString();
}
// 页码加一
private void pageAdd()
{
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstBlue.png";
pictureBox_first.Enabled = true;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preBlue.png";
pictureBox_pre.Enabled = true;
int i = Convert.ToInt32(textBox_thisPage.Text);
int maxPageNum = Convert.ToInt32(label_pageNum.Text);
if (i < maxPageNum)
{
i++;
}
textBox_thisPage.Text = i.ToString();
if (i >= maxPageNum)
{
pictureBox_next.Enabled = false;
pictureBox_next.ImageLocation = common.srvPath_Resource + "nextGray.png";
pictureBox_last.Enabled = false;
pictureBox_last.ImageLocation = common.srvPath_Resource + "lastGray.png";
}
else
{
pictureBox_next.Enabled = true;
pictureBox_next.ImageLocation = common.srvPath_Resource + "nextBlue.png";
pictureBox_last.Enabled = true;
pictureBox_last.ImageLocation = common.srvPath_Resource + "lastBlue.png";
}
}
// 页码减一
private void pageMiu()
{
pictureBox_next.ImageLocation = common.srvPath_Resource + "nextBlue.png";
pictureBox_next.Enabled = true;
pictureBox_last.ImageLocation = common.srvPath_Resource + "lastBlue.png";
pictureBox_last.Enabled = true;
int i = Convert.ToInt32(textBox_thisPage.Text);
int maxPageNum = Convert.ToInt32(label_pageNum.Text);
if (i > 1)
{
i--;
}
textBox_thisPage.Text = i.ToString();
if (i == 1)
{
pictureBox_pre.Enabled = false;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preGray.png";
pictureBox_first.Enabled = false;
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstGray.png";
}
else
{
pictureBox_pre.Enabled = true;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preBlue.png";
pictureBox_first.Enabled = true;
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstBlue.png";
}
}
// 重新选择每页条数触发事件
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox_thisPage.Text = "1";
pictureBox1_Click(sender, e);
}
// 下一页按钮
private void pictureBox4_Click(object sender, EventArgs e)
{
pageJump();
pageAdd();
pageJump();
}
// 回到前一页按钮
private void pictureBox2_Click(object sender, EventArgs e)
{
pageJump();
pageMiu();
pageJump();
}
// 回到第一页按钮
private void pictureBox1_Click(object sender, EventArgs e)
{
textBox_thisPage.Text = "1";
pictureBox_pre.Enabled = false;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preGray.png";
pictureBox_first.Enabled = false;
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstGray.png";
pictureBox_next.Enabled = true;
pictureBox_next.ImageLocation = common.srvPath_Resource + "nextBlue.png";
pictureBox_last.Enabled = true;
pictureBox_last.ImageLocation = common.srvPath_Resource + "lastBlue.png";
pageJump();
}
// 跳至最后一页按钮
private void pictureBox3_Click(object sender, EventArgs e)
{
textBox_thisPage.Text = label_pageNum.Text.ToString();
pictureBox_pre.Enabled = true;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preBlue.png";
pictureBox_first.Enabled = true;
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstBlue.png";
pictureBox_next.Enabled = false;
pictureBox_next.ImageLocation = common.srvPath_Resource + "nextGray.png";
pictureBox_last.Enabled = false;
pictureBox_last.ImageLocation = common.srvPath_Resource + "lastGray.png";
pageJump();
}
// 初始化分页工具条按钮
private void resetBtn()
{
pictureBox_first.ImageLocation = common.srvPath_Resource + "firstGray.png";
pictureBox_first.Enabled = false;
pictureBox_pre.ImageLocation = common.srvPath_Resource + "preGray.png";
pictureBox_pre.Enabled = false;
}
// 当前页输入框手动填写页码跳转事件
private void pageJump()
{
string str1 = textBox_thisPage.Text.ToString();
string maxStr = label_pageNum.Text.ToString();
if (!server.isEmptyS(str1))
{
if ((!Regex.IsMatch(str1, @"^(^\d{1,8})$", RegexOptions.IgnoreCase)))
{
MessageBox.Show("请输入8位以内数字格式页码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.textBox_thisPage.Text = "1";
this.textBox_thisPage.Focus();
}
else
{
int thisPage = Convert.ToInt32(str1);
int maxPage = Convert.ToInt32(maxStr);
if (thisPage > maxPage)
{
MessageBox.Show("输入页码超出最大页码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.textBox_thisPage.Text = "1";
this.textBox_thisPage.Focus();
}
else
{
page();
loadList();
page();
}
}
}
}
// 光标从当前页码输入框移除触发页码跳转事件
private void textBox_thisPage_Leave(object sender, EventArgs e)
{
pageJump();
}
// 加载List方法
public void loadList()
{
// Dgv 加载数据方法
}
#endregion
}
}