Summary of a paging control in winform

Source: Internet
Author: User

After finishing this article, I feel it is necessary to develop win form.ProgramA paging program that is frequently used is also summarized. Although you are familiar with it, you may not know how to use it if you are not familiar with it. Therefore, a demo is prepared in this article. At the end of this article, you can download and try it out.

 

1. engineering and project structure

In this example, you can use ibatisnet to perform a simple data query.

 

2. Paging Control

(1) Paging

In DOTNET. DOTNET. common. the winform project has a user control pagercontrol. Its Design view mainly includes four linklabels, a digital text box, a button control, and several label controls related to the display statistics. In this example, the page display effect is as follows:

The controls are ugly, so there is no talent for art or design.

(2) control usage

// Bind page number information pagercontrol pager = new pagercontrol (this. currentpage, this. recordsperpage, this. totalcount, "jump"); pager. currentpagechanged + = new eventhandler (pager_currentpagechanged); // This event triggered by page number change. panelpager. controls. add (PAGER); // Add this control to the Panel container

When using this control, you only need to create a new control object, and then pass in the correct parameters, such as the number of records on each page on the current page, the total number of records in the data set and the text displayed on the button.

(3) how to correctly PASS Parameters and pagination

For paging, it is important to pass in the query conditions and page number parameters. Query condition our mainform has a ready-made input text box. How can we pass the current page number parameter? There is no doubt that the combination of four linklabels, a number input box, and a button will change the number of the current page. Therefore, we need to perform some currentpage processing in the corresponding events:

# Region-related control event private void lnkfirst_linkclicked (Object sender, Volume e) {currentpage = 1; drawcontrol ();} private void lnkprev_linkclicked (Object sender, Volume e) {currentpage = math. max (1, currentpage-1); drawcontrol ();} private void lnknext_linkclicked (Object sender, linklabellinkclickedeventargs e) {currentpage = math. min (pagecount, currentpage + 1); drawcontrol ();} private void lnklast_linkclicked (Object sender, linklabellinkclickedeventargs e) {currentpage = pagecount; drawcontrol ();} /// <summary> /// enter key function /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void txtpagenum_keypress (Object sender, keypresseventargs e) {string strnum = this.txt pagenum. text. trim (); If (E. keychar = (char) keys. enter & regutil. ispositivenumber (strnum) {currentpage = int. parse (strnum); drawcontrol ();}} /// <summary> // page size limit /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void txtpagenum_textchanged (Object sender, eventargs e) {string strnum = this.txt pagenum. text. trim (); If (strnum. length> 0 & regutil. ispositivenumber (strnum) & Int. parse (strnum)> pagecount) {txtpagenum. TEXT = pagecount. tostring ();}} /// <summary> /// jump // </Summary> /// <Param name = "sender"> </param> /// <Param name =" E "> </param> private void btngo_click (Object sender, eventargs e) {string strnum = this.txt pagenum. text. trim (); If (regutil. ispositivenumber (strnum) = false) {return;} currentpage = int. parse (strnum); drawcontrol () ;}# endregion

we noticed the drawcontrol method, which is a very important function:

/// <Summary> // display the page control /// </Summary> private void drawcontrol () {This. btngo. TEXT = This. jumptext; this. lblcurrentpage. TEXT = currentpage. tostring (); this. lblpagecount. TEXT = pagecount. tostring (); this. lbltotalcount. TEXT = totalcount. tostring (); this. lblrecperpg. TEXT = recordsperpage. tostring (); If (currentpagechanged! = NULL) {currentpagechanged (this, null); // triggers the delegate event when the number of the current page changes} setformctrenabled (); If (pagecount = 1) // there is only one page {This. lnkfirst. enabled = false; this. lnkprev. enabled = false; this. lnknext. enabled = false; this. lnklast. enabled = false; this. btngo. enabled = false;} else if (currentpage = 1) // The first page {This. lnkfirst. enabled = false; this. lnkprev. enabled = false; // This. lnkfirst. forecolor = color. gray; // This. lnkprev. forecolor = color. gray;} else if (currentpage = This. pagecount) // The last page {This. lnknext. enabled = false; this. lnklast. enabled = false ;}}

It triggers the following delegate event conditionally:

 
Public event eventhandler currentpagechanged;

We can fully understand this: When we click the button or linklabel, the current page number changes, so we need to do something new, such as querying. The control is triggered by triggering the currentpagechanged event:

 
If (currentpagechanged! = NULL) {currentpagechanged (this, null); // The delegate event is triggered when the number on the current page changes}

As for what to do when the currentpagechanged delegate is triggered, it is what the function in each specific form is to do. For example, in the demo in this article, in mainform, we define the following method to assign values to currentpagechanged of Pager:

Private void pager_currentpagechanged (Object sender, eventargs e) {pagercontrol pager = sender as pagercontrol; If (PAGER = NULL) {return;} bindresults (pager. currentpage); // query data and bind it by PAGE}

When the event Delegate of the paging control is triggered, the bindresults function queries and binds data through the current page number of pager. In this case, it may be good for you to understand delegation and events in a general sense.

In general, the paging Effect of winform is simpler than that of webform, because it does not need to consider changes in the status of some parameters.

 

Download an example: ibatiswinapp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.