Click here to Skip to main content
15,887,477 members
Home / Discussions / C#
   

C#

 
QuestionMaster-details Issue: Works with EW3 But Not with EW4 Pin
SaintNek30-Nov-12 5:08
SaintNek30-Nov-12 5:08 
AnswerRe: Master-details Issue: Works with EW3 But Not with EW4 Pin
Eddy Vluggen1-Dec-12 5:52
professionalEddy Vluggen1-Dec-12 5:52 
AnswerRe: Master-details Issue: Works with EW3 But Not with EW4 Pin
SaintNek4-Dec-12 8:35
SaintNek4-Dec-12 8:35 
QuestionA MEF question Pin
John T.Emmatty30-Nov-12 4:50
John T.Emmatty30-Nov-12 4:50 
AnswerRe: A MEF question Pin
Simon_Whale30-Nov-12 4:59
Simon_Whale30-Nov-12 4:59 
QuestionRe: A MEF question Pin
Richard Deeming30-Nov-12 5:35
mveRichard Deeming30-Nov-12 5:35 
AnswerRe: A MEF question Pin
John T.Emmatty30-Nov-12 20:00
John T.Emmatty30-Nov-12 20:00 
QuestionC# WinForms - Printing Multiple Pages Pin
Matt U.30-Nov-12 4:29
Matt U.30-Nov-12 4:29 
I have a custom control called EditorSection which contains a Button control for the header and a RichTextBox control. I have another custom control called EditorContainer which can contain multiple EditorSection controls. In the EditorContainer I have a Button control for "Print All", which obviously should print all of the EditorSection controls' content.

I created a class called "PrintSection", which stores the title (header text) for each section as well as a string array for the "Lines" property of the RichTextBox in each EditorSection custom control. All is fine so far.

When I click "Print All" I would like to print each section's header title and then prints that section's content below it, which will sometimes span more than one page. This is where I have a problem. I'm not sure how to achieve multi-page printing with this setup.

Here is how I populate a List<printsection> object:

C#
this._printSections = new List<PrintSection>();
// Copy each section's content
foreach (Control child in sectionsPanel.Controls)
{
    if (child is EditorSection)
    {
        PrintSection section = new PrintSection();
        section.Name = ((EditorSection)child).SectionName;
        section.Lines = ((EditorSection)child).GetLines();
        this._printSections.Add(section);
    }
}


The "GetLines()" method returns the "Lines" property for the RichTextBox in the EditorSection control, while "SectionName" is the section's header title.

Here is what I have tried so far with printing:

C#
int sectionIndex = 0, lineIndex = 0;

void document_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    Font font = new Font(SystemFonts.DefaultFont.FontFamily.Name, 9.5f);
    Font headerFont = new System.Drawing.Font(font.FontFamily.Name, 12.0f, FontStyle.Bold);

    float yPos = 0;
    PrintSection section = this._printSections.ElementAt(sectionIndex);
    {
        // Print the header
        g.DrawString(section.Name, headerFont, Brushes.SteelBlue, 0, yPos);
        yPos += (g.MeasureString(section.Name, headerFont).Height + 4f);
        g.DrawLine(Pens.LightSlateGray, 0, yPos, g.MeasureString(section.Name, headerFont).Width * 2, yPos);
        yPos += (g.MeasureString(section.Name, headerFont).Height);

        // Print the section's lines
        for (int l = lineIndex; l < section.Lines.Count(); l++)
        {
            string line = section.Lines[l];
            g.DrawString(line, font, Brushes.Black, 0, yPos);
            yPos += (g.MeasureString(line, font).Height);

            lineIndex++;
        }

        yPos += 32f;
        if (sectionIndex < (this._printSections.Count - 1))
        {
            sectionIndex++;
            e.HasMorePages = true;
        }
    }
}


The "sectionIndex" variable holds the zero-based index of the section currently being printed. I am able to print each section on its own page, which is desirable. However, I would eventually like to allow for printing continuously without making a new page for each section, as an option to the user.

The problem I am facing is this. How do I print the section's lines on more than one page if it doesn't fit on one page? I have the "yPos" variable which is the current position on the y-axis for printing. I have tried "if (yPos > pageHeight) { e.HasMorePages = true; }" but it doesn't print the remaining lines on a new page.

What am I doing wrong?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.

AnswerRe: C# WinForms - Printing Multiple Pages Pin
BobJanova30-Nov-12 4:34
BobJanova30-Nov-12 4:34 
GeneralRe: C# WinForms - Printing Multiple Pages Pin
Matt U.30-Nov-12 4:48
Matt U.30-Nov-12 4:48 
QuestionFind the differences between two images for screen sharing apps Pin
Tridip Bhattacharjee30-Nov-12 3:13
professionalTridip Bhattacharjee30-Nov-12 3:13 
AnswerRe: Find the differences between two images for screen sharing apps Pin
Dave Kreskowiak30-Nov-12 4:07
mveDave Kreskowiak30-Nov-12 4:07 
AnswerRe: Find the differences between two images for screen sharing apps Pin
BobJanova30-Nov-12 4:32
BobJanova30-Nov-12 4:32 
GeneralRe: Find the differences between two images for screen sharing apps Pin
Richard Deeming30-Nov-12 4:42
mveRichard Deeming30-Nov-12 4:42 
QuestionAdding/Removing multiple IP addresses to NIC Pin
lorenzo.santoro29-Nov-12 21:07
lorenzo.santoro29-Nov-12 21:07 
AnswerRe: Adding/Removing multiple IP addresses to NIC Pin
Dave Kreskowiak30-Nov-12 4:05
mveDave Kreskowiak30-Nov-12 4:05 
GeneralRe: Adding/Removing multiple IP addresses to NIC Pin
lorenzo.santoro12-Dec-12 5:26
lorenzo.santoro12-Dec-12 5:26 
QuestionDetecting Webcam Pin
sarang_k29-Nov-12 19:51
sarang_k29-Nov-12 19:51 
AnswerRe: Detecting Webcam Pin
Pete O'Hanlon29-Nov-12 20:35
mvePete O'Hanlon29-Nov-12 20:35 
GeneralRe: Detecting Webcam Pin
sarang_k2-Dec-12 18:17
sarang_k2-Dec-12 18:17 
GeneralRe: Detecting Webcam Pin
Pete O'Hanlon2-Dec-12 19:23
mvePete O'Hanlon2-Dec-12 19:23 
GeneralRe: Detecting Webcam Pin
sarang_k9-Dec-12 22:41
sarang_k9-Dec-12 22:41 
QuestionResourceBundle class in C#? Pin
LAPEC29-Nov-12 12:17
LAPEC29-Nov-12 12:17 
AnswerRe: ResourceBundle class in C#? Pin
Pete O'Hanlon29-Nov-12 12:53
mvePete O'Hanlon29-Nov-12 12:53 
QuestionHow do you properly deal with ContextSwitchDeadlock or DisconnectedContext? Pin
turbosupramk329-Nov-12 11:13
turbosupramk329-Nov-12 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.