1、创建用户控件,添加“上一页”、“下一页”和多个页码,全部为LinkLabel控件 效果 2、实现代码(假设8页一屏) using System; using System.Drawing; using System.Windows.Forms; namespace km.Controls { /// <summary> /// 分页控件 /// </summary> public partial class PageList : UserControl { /// <summary> /// 分页标签数量 /// </summary> private const int count = 8; /// <summary> /// 总页数 /// </summary> public int totalpage { get; set; } /// <summary> /// 当前页 /// </summary> private int cpage = 1; /// <summary> /// 循环值 /// </summary> private int i; /// <summary> /// 页码控件列表下标 /// </summary> private int listFlag = 0; /// <summary> /// 页码控件列表 /// </summary> private LinkLabel[] linkList; /// <summary> /// 链接字体 /// </summary> private Font linkFont = new System.Drawing.Font("Tahoma", 9.0f, FontStyle.Underline); /// <summary> /// 普通字体 /// </summary> private Font regularFont = new System.Drawing.Font("Tahoma", 9.0f, FontStyle.Regular); /// <summary> /// 链接字颜色 /// </summary> private Color linkColor = Color.Blue; /// <summary> /// 普通字颜色 /// </summary> private Color regularColor = Color.FromArgb(38, 38, 38); /// <summary> /// 页标签点击事件 /// </summary> public event EventHandler ClickPage; public PageList() { InitializeComponent(); linkList = new LinkLabel[] { linkLabel1, linkLabel2, linkLabel3, linkLabel4, linkLabel5, linkLabel6, linkLabel7, linkLabel8 }; } /// <summary> /// 设置页码 /// </summary> public void SetPage() { if (totalpage <= count) { //总页数小于十页 for (i = 1; i <= totalpage; i++) { listFlag = i - 1; linkList[listFlag].Text = i.ToString(); if (i == cpage) { linkList[listFlag].Font = regularFont; linkList[listFlag].ForeColor = regularColor; } else { linkList[listFlag].Font = linkFont; linkList[listFlag].ForeColor = linkColor; } } linkLabelPrevious.Text = ""; linkLabelNext.Text = ""; } if (totalpage > count) { if ((cpage - 1) / count == 0) //第一页 { for (i = 1; i <= count; i++) { listFlag = i - 1; linkList[listFlag].Text = i.ToString(); if (i == cpage) { linkList[listFlag].Font = regularFont; linkList[listFlag].ForeColor = regularColor; } else { linkList[listFlag].Font = linkFont; linkList[listFlag].ForeColor = linkColor; } } linkLabelPrevious.Text = ""; linkLabelNext.Text = ">>"; } else if ((cpage - 1) / count == totalpage / count) //最后一页 { for (i = (cpage - 1) / count * count + 1; i <= (cpage - 1) / count * count + count; i++) { listFlag = (i - 1) % count; if (i > totalpage) //超出总页数的按钮设为空 { linkList[listFlag].Text = ""; } else { linkList[listFlag].Text = i.ToString(); if (i == cpage) { linkList[listFlag].Font = regularFont; linkList[listFlag].ForeColor = regularColor; } else { linkList[listFlag].Font = linkFont; linkList[listFlag].ForeColor = linkColor; } } } linkLabelPrevious.Text = "<<"; linkLabelNext.Text = ""; } else //其他页 { for (i = (cpage - 1) / count * count + 1; i <= (cpage - 1) / count * count + count; i++) { listFlag = (i - 1) % count; linkList[listFlag].Text = i.ToString(); if (i == cpage) { linkList[listFlag].Font = regularFont; linkList[listFlag].ForeColor = regularColor; } else { linkList[listFlag].Font = linkFont; linkList[listFlag].ForeColor = linkColor; } } linkLabelPrevious.Text = "<<"; linkLabelNext.Text = ">>"; } } if (null != ClickPage) ClickPage.Invoke(cpage, null); } /// <summary> /// 页码点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLabel_Click(object sender, EventArgs e) { LinkLabel linkLabel = sender as LinkLabel; if (linkLabel.Text.Equals("")) return; cpage = Convert.ToInt32(linkLabel.Text); SetPage(); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLabelPrevious_Click(object sender, EventArgs e) { if (linkLabelPrevious.Text.Equals("")) return; cpage = cpage - cpage % count; cpage = cpage - count + 1; SetPage(); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLabelNext_Click(object sender, EventArgs e) { if (linkLabelNext.Text.Equals("")) return; cpage = cpage + count; cpage = cpage - cpage % count + 1; SetPage(); } } }