C# development WinForm page controls

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

 The WinFormPager.dll control download WinFormPager source code download

The following is the call paging controls WinFormPager method:

//The first step: the number of returned records specified

WinFormPager1.RecordCount = return the number of records;

//The second step: the method performs binding DataGridView in the control's PageChanged event.

private void winFormPager1_PageChanged()

 {

dataGridView1.DataSource = GetList(winFormPager1.PageSize,winFormPager1.CurrentPage);//GetList access database recording method, not the

 }

//Note: the completion of the above steps can successfully call, the rest of the PageSize attribute can be set in the property browser

The following is the complete code paging controls WinformPager:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using DevComponents.DotNetBar;

namespace WinFormPager

{

public partial class WinFormPager : UserControl

{

int currentPage = 1;//The current page

   /// <summary>

/// The current page

/// </summary>

[Description("The current page "), Category (" paging settings")]

public int CurrentPage

{

get { return currentPage; }

set { currentPage = value; }

}

int pageSize = 10;//The page displays the number of

/// <summary>

/// The page displays the number of

/// </summary>

[Description("The page displays the number of "), Category (" paging settings")]

public int PageSize

{

get { return pageSize; }

set { pageSize = value; }

}

int pageTotal = 0;//How many pages

  /// <summary>

/// How many pages

/// </summary>

[Description("How many pages "), Category (" paging settings")]

public int PageTotal

{

get { return pageTotal; }

set { pageTotal = value; }

}

int currentGroup = 1;//The current group

/// <summary>

/// The current group

/// </summary>

[Description("The current group "), Category (" paging settings")]

public int CurrentGroup

{

get { return currentGroup; }

set { currentGroup = value; }

}

int groupSize = 10;//Each display pages

/// <summary>

/// Each display pages

/// </summary>

[Description("Each display pages "), Category (" paging settings")]

public int GroupSize

{

get { return groupSize; }

set { groupSize = value; }

}

int groupTotal = 0;//How many groups

/// <summary>

/// How many groups

/// </summary>

[Description("How many groups "), Category (" paging settings")] 

public int GroupTotal

{

get { return groupTotal; }

set { groupTotal = value; }

}

  /// <summary>

/// The total number of records

/// </summary>

private int recordCount;//The total number of records

[Description("The number of "total record), Category (" paging settings")]

public int RecordCount

{

get { return recordCount; }

set

{

recordCount = value;

InitData();// Initialization data

PageChanged();//The current page change event

}

}

private int buttonWidth = 20;//The width of the button

/// <summary>

/// The width of the button

/// </summary>

[Description("The width of the button "), Category (" paging settings")]

public int ButtonWidth

{

get { return buttonWidth; }

set { buttonWidth = value; }

}

private int buttonHeight = 23;//Button height

   /// <summary>

/// Button height

/// </summary>

[Description("Button height "), Category (" paging settings")]

public int ButtonHeight

{

get { return buttonHeight; }

set { buttonHeight = value; }

}

private int buttonDistance = 0;//Button distance

/// <summary>

/// Button distance

/// </summary>

[Description("The distance between the button "), Category (" paging settings")]

public int ButtonDistance

{

get { return buttonDistance; }

set { buttonDistance = value; }

}

List<Control> listControl = new List<Control>();//Paging button collection

public delegate void PageChangeDelegate();

/// <summary>

/// The current page is changed when the event occurs

/// </summary>

[Description("The current page change events "), Category (" paging settings")]

public event PageChangeDelegate PageChanged;

public WinFormPager()

{

InitializeComponent();

PageChanged = SetBtnPrePageAndBtnNextPage;

PageChanged();

}

/// <summary>

/// Initialization data

/// </summary>

private void InitData()

{

PageTotal = RecordCount / PageSize;//How many pages

if (RecordCount % PageSize != 0)

{

PageTotal++;

}

GroupTotal = PageTotal / GroupSize;//How many groups

if (PageTotal % GroupSize != 0)

{

GroupTotal++;

}

for (int i = 0; i <PageTotal; i++)

{

cmbPage.Items.Add((i + 1).ToString());//Add a drop-down box value

}

BuildPageControl();//Create a page number buttons

}

   /// <summary>

/// Create a page number buttons

/// </summary>

private void BuildPageControl()

{

int x = 0;//Button the abscissa

int y = 0;//Button ordinate

int num = 0;//Button number

for (int i = GroupSize * (CurrentGroup - 1); i <GroupSize * CurrentGroup; i++)

{

if (i + 1 > PageTotal)

{

break;

}

num++;

}

int xBtnPreGroup = x + (ButtonWidth + ButtonDistance) * num;//BtnPerGroup abscissa

  //Specify a group of a group of a coordinate

btnPreGroup.Location = new Point(xBtnPreGroup, y);

btnNextGroup.Location = new Point(btnPreGroup.Location.X + btnPreGroup.Width + ButtonDistance, y);

btnPrePage.Location = new Point(btnNextGroup.Location.X + btnNextGroup.Width + ButtonDistance, y);

btnNextPage.Location = new Point(btnPrePage.Location.X + btnPrePage.Width + ButtonDistance, y);

cmbPage.Location = new Point(btnNextPage.Location.X + btnNextPage.Width + ButtonDistance, y+(ButtonHeight-cmbPage.Height)/2);

btnGo.Location = new Point(cmbPage.Location.X + cmbPage.Width + ButtonDistance, y);

//Set the controls the width, height

this.Width = btnGo.Location.X + btnGo.Width;

this.Height = ButtonHeight;

btnPreGroup.Height = ButtonHeight;

btnNextGroup.Height = ButtonHeight;

btnPrePage.Height = ButtonHeight;

btnNextPage.Height = ButtonHeight;

cmbPage.Height = ButtonHeight;

btnGo.Height = ButtonHeight;

//Loop through the removal of controls

foreach (Control c in listControl)

{

this.Controls.Remove(c);

}

listControl = new List<Control>();

ButtonX button = null;

//Build cycle control

for (int i = GroupSize * (currentGroup - 1); i <GroupSize * CurrentGroup; i++)

{

if (i + 1 > PageTotal)

{

break;

}

button = new ButtonX();

button.Text = (i + 1).ToString();

button.Width = ButtonWidth;

button.Height = ButtonHeight;

button.ColorTable = DevComponents.DotNetBar.eButtonColor.OrangeWithBackground;

button.Location = new Point(x, y);

button.Click += new EventHandler(button_Click);

this.Controls.Add(button);

button.BringToFront();

listControl.Add(button);//Add set into the paging buttons

x += ButtonWidth + ButtonDistance;

}

//A group is available

if (CurrentGroup == 1)

{

btnPreGroup.Enabled = false;

}

else

{

btnPreGroup.Enabled = true;

}

//The next group is available

if (CurrentGroup == GroupTotal)

{

btnNextGroup.Enabled = false;

}

else

{

btnNextGroup.Enabled = true;

}

}

/// <summary>

/// Digital button tabs

/// </summary>

private void button_Click(object sender, EventArgs e)

{

CurrentPage = int.Parse((sender as ButtonX).Text);

PageChanged();

}

/// <summary>

/// Set the previous page, the next page is available and the current page button font color

/// </summary>

private void SetBtnPrePageAndBtnNextPage()

{

//A page is available

if (CurrentPage == 1)

{

btnPrePage.Enabled = false;

}

else

{

btnPrePage.Enabled = true;

}

//The next page is available

if (CurrentPage == PageTotal)

{

btnNextPage.Enabled = false;

}

else

{

btnNextPage.Enabled = true;

}

//Sets the number of page button text color

foreach (Control c in this.Controls)

{

//The current page font for red

if (c.Text == CurrentPage.ToString() && c is ButtonX)

{

c.ForeColor = Color.Red;

}

else

{

c.ForeColor = Color.Blue;

}

}

}

/// <summary>

/// A group of

/// </summary>

private void btnPreGroup_Click(object sender, EventArgs e)

{

CurrentGroup--;

BuildPageControl();

CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;

PageChanged();

}

  /// <summary>

/// The next group of

/// </summary>

private void btnNextGroup_Click(object sender, EventArgs e)

{

CurrentGroup++;

BuildPageControl();

CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;

PageChanged();

}

  /// <summary>

/// On one page

/// </summary>

private void btnPrePage_Click(object sender, EventArgs e)

{

//If this is the first page of the current group, directly on a group

if (CurrentPage == GroupSize * (CurrentGroup - 1) + 1)

{

CurrentGroup--;

BuildPageControl();

CurrentPage --; ;

PageChanged();

return;

}

CurrentPage--;

PageChanged();

}

/// <summary>

/// The next page

/// </summary>

private void btnNextPage_Click(object sender, EventArgs e)

{

//If it is the last page of the current group, directly to the next group

if (CurrentPage == GroupSize * (CurrentGroup - 1) + GroupSize)

{

btnNextGroup_Click(null, null);

return;

}

CurrentPage++;

PageChanged();

}

  /// <summary>

/// Turn to page

/// </summary>

private void btnGo_Click(object sender, EventArgs e)

{

try

{

CurrentPage = int.Parse(cmbPage.Text);

PageChanged();

}

catch

{

MessageBox.Show("Please enter a number");

}

}

}

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cher at November 14, 2013 - 10:10 PM