Skip to main content
Email Password   helpLost your password?

Demo application

List Print Preview

Introduction

Some time during my development works I had the need of printing the contents of a ListView control. So I wrote down the code for a custom control, named PrintableListView, that solved the following two problems:

Using the code

PrintableListView control is derived from System.Windows.Forms.ListView, so use it like the native ListView control. The additional functionalities are those for print and for print preview.

// call the print method for displaying the standard print dialog.

this.m_list.Print();


// call the print preview method for displaying

// the standard print preview dialog

this.m_list.PrintPreview();

To turn on the �Fit to page� option, use the FitToPage property of the control.

this.m_list.FitToPage = true;

To set the title that appears on the page header, use the Title property of the control.

Points of Interest

If you think about the ListView control, you will note that we have all the information we need to print it in the simplest way. We have the columns width, the fonts, the rows height and so on. All we have to do is to get all these information and play a little with the Graphics instance that represents the printing surface. What follows is the way I solved the problems listed in the Introduction.

Multi page print

This is quite simple. When we are printing the current column of a row, we check whether there is enough room to print it. If not, we mark that column as the first one for the next page. That�s it.

Fit to page

This point is just a little bit more complex compared to the previous one. If the total width of the list is greater than the width of the available space, we have to calculate a scaling factor, given by the list width divided by the available space. For simplicity, in my implementation, I converted all the measures in units of hundredths of an inch. To set the scale factor and unit of measure is sufficient to change the coordinate system of the Graphics object passed to the handler of the PrintPage event of the PrintDocument object.

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
    �
    if (m_nStartCol==0 && m_bFitToPage && m_fListWidth>rectBody.Width)
    {
        // Calculate scale factor

        fScale = rectBody.Width / m_fListWidth;
    }
    �
    �
    // Setting scale factor and unit of measure

    g.ScaleTransform(fScale, fScale);
    g.PageUnit = GraphicsUnit.Inch;
    g.PageScale = 0.01f;
    �
}
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralImages in ListView Print Preview Pin
hellbinder
8:49 5 Nov '09  
GeneralPrinting a ListView that contains groups Pin
Mark F.
10:32 23 Mar '09  
QuestionWhat's the license associated with your code? Pin
Zaki Saadeh
7:24 27 May '08  
Questionhow i can bind treeview With My Table in Database Pin
waleedmaklad
10:35 9 Dec '07  
Questionhow can i bind my table with treeview In C# Pin
waleed
10:32 9 Dec '07  
Generalhow to select printer? Pin
Suha
13:32 18 Nov '07  
QuestionRight-to-left Pin
SohailB
3:28 17 Nov '07  
GeneralThanks, but Pin
bart_net_master
4:17 20 Aug '07  
Generalhttp://puzzle.tangoing.info/ Pin
http://puzzle.tangoing.info/
11:29 7 Dec '07  
GeneralGreat article...Thanks... Pin
Abhishek_Gajab
3:42 14 Aug '07  
GeneralInvalidArgument=Value of '1' is not valid for 'index'. Pin
Jordan Marrazzo
18:59 4 Jul '07  
GeneralRe: InvalidArgument=Value of '1' is not valid for 'index'. Pin
Jordan Marrazzo
19:33 4 Jul '07  
QuestionHow to print selected pages using this Printable ListView control Pin
durgamic
2:08 10 Apr '07  
GeneralHow can i select required pages Pin
durgamic
2:05 10 Apr '07  
QuestionPrint 2 listviews on same page [modified] Pin
csfong33
19:10 14 Mar '07  
GeneralListview print Pin
Georgiio
11:20 7 Jan '07  
GeneralPrinting with diffent dongs Pin
enlikil
6:44 27 Oct '06  
GeneralAbout language Pin
jabulino
21:44 19 Mar '06  
QuestionColumn order Pin
Colin Parker
2:07 11 Oct '05  
AnswerRe: Column order Pin
Mircea Puiu
2:43 11 Oct '05  
GeneralRe: Column order Pin
Colin Parker
2:54 11 Oct '05  
GeneralRe: Column order Pin
Mircea Puiu
3:43 11 Oct '05  
Generalpippaguru... Pin
JonathanLivingstone
21:41 19 Sep '05  
GeneralRe: pippaguru... Pin
Matteo D'Avena
21:57 19 Sep '05  


Last Updated 19 Sep 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009