WinForm增加分页的用户控件
1.新建用户控件:
选择项目-------添加--------新建项-------Windows Forms-------用户控件------命名为ViewPager.cs。
2.增加分页需要的控件元素
3.代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RFTobacoo.UserController
{
public partial class ViewPager : UserControl
{
private int record = 0;
/// <summary>
/// 总记录数
/// </summary>
public int Record
{
get { return record; }
set
{
record = value;
InitPageInfo();
}
}
private int pageSize = 10;
/// <summary>
/// 每页条数
/// </summary>
public int PageSize
{
get { return pageSize; }
set { pageSize = Convert.ToInt32(cmb_pageCount.Text); }
}
private int currentPage = 1;
/// <summary>
/// 当前页
/// </summary>
public int CurrentPage
{
get { return currentPage; }
set { currentPage = value; }
}
public int pageNum = 0;
/// <summary>
/// 总页码
/// </summary>
public int PageNum
{
get
{
if (Record == 0|| PageSize == 0)
{
pageNum = 0;
}
else
{
if (Record % PageSize > 0)
{
pageNum = Record / PageSize + 1;
}
else
{
pageNum = Record / PageSize;
}
}
return pageNum;
}
}
//定义委托
public delegate void BindHandle();
/// <summary>
/// 绑定数据源事件
/// </summary>
public event BindHandle BindSource;
public ViewPager()
{
InitializeComponent();
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
if (Record > 0)
{
if (CurrentPage == 1)
{
MessageBox.Show("当前已经是首页");
return;
}
else
{
CurrentPage = 1;
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
}
}
private void btnPre_Click(object sender, EventArgs e)
{
if (Record > 0)
{
if (CurrentPage == 1)
{
MessageBox.Show("当前已经是首页");
return;
}
else
{
CurrentPage = CurrentPage - 1;
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (Record > 0)
{
if (CurrentPage == PageNum)
{
MessageBox.Show("当前已经是末页");
return;
}
else
{
CurrentPage = CurrentPage + 1;
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (Record > 0)
{
if (CurrentPage == PageNum)
{
MessageBox.Show("当前已经是末页");
return;
}
else
{
CurrentPage = PageNum;
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
}
}
private void InitPageInfo()
{
if (Record == 0 || (Record > 0 && CurrentPage > pageNum))
{
CurrentPage = 1;
}
lblInfo.Text = string.Format("共 {0} 条记录 共 {1} 页 当前第 {2} 页", Record, PageNum, CurrentPage);
txtPage.Text = CurrentPage.ToString();
}
private void btnGo_Click(object sender, EventArgs e)
{
if (Record > 0)
{
if (!string.IsNullOrEmpty(txtPage.Text) && !Regex.IsMatch(txtPage.Text, @"^[\d]*$"))
{
MessageBox.Show("请正确填写页码!");
return;
}
int page = Convert.ToInt32(txtPage.Text);
if (page == 0)
{
page = 1;
}
if (page > PageNum)
{
page = PageNum;
}
CurrentPage = page;
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
}
private void ViewPager_Load(object sender, EventArgs e)
{
if (BindSource != null)
{
BindSource();
InitPageInfo();
}
}
private void cmb_pageCount_BindingContextChanged(object sender, EventArgs e)
{
}
private void cmb_pageCount_SelectedIndexChanged(object sender, EventArgs e)
{
pageSize = Convert.ToInt32(cmb_pageCount.Text.Trim());
BindSource();
}
}
}