when i print the items in my listboxes it just keeps going and when it gets to the end of the page, it justs cuts off the rest. how do i get it to go to the next page. This is the code i have
private void docPrint_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
float xPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
string line2 = null;
m_objFont = new Font("Arial", 12);
// Calculate the number of lines per page.
linesPerPage = e.MarginBounds.Height /
m_objFont.GetHeight(e.Graphics);
// Print each line of the file.
foreach (Control objControl in this.Controls)
{
if (objControl.GetType().Name != "Button" && objControl.GetType().Name != "MenuStrip" && objControl.Name != "lblItemName" && objControl.Name != "lblItemValue" )
{
line = objControl.Text;
switch (objControl.Name)
{
case "lblSum":
e.Graphics.DrawRectangle(Pens.Black, lblSum.Location.X + leftMargin, lblSum.Location.Y + topMargin - 4, lblSum.Width, lblSum.Height);
m_objFont = objControl.Font;
break;
default:
m_objFont = objControl.Font;
m_objFont = new Font("Arial", 12);
break;
}
xPos = leftMargin + objControl.Location.X;
yPos = topMargin + objControl.Location.Y;
e.Graphics.DrawString(line, m_objFont, Brushes.Black, xPos, yPos);
}
}
while (count < linesPerPage && nextItem < lstResult.Items.Count && nextItem < lstResult.Items.Count)
{
m_objFont = new Font("Arial", 12, FontStyle.Underline);
yPos = topMargin + 210 + (count *
m_objFont.GetHeight(e.Graphics));
line = lstResult.Items[nextItem].ToString();
line2 = lstResult2.Items[nextItem].ToString();
e.Graphics.DrawString(line, m_objFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
e.Graphics.DrawString(line2, m_objFont, Brushes.Black, leftMargin + 500, yPos, new StringFormat());
count++;
nextItem++;
}
// If more lines exist, print another page.
if (nextItem < lstResult.Items.Count - 1 && nextItem < lstResult2.Items.Count - 1)
e.HasMorePages = true;
else
{
e.HasMorePages = false;
nextItem = 0;
}
}
private void mnuFilePrint_Click(object sender, EventArgs e)
{
PrintDocument docPrint = new PrintDocument();
docPrint.PrintPage += new PrintPageEventHandler(docPrint_PrintPage);
if (PrinterSettings.InstalledPrinters.Count == 0)
{
ErrorMessage();
return;
}
docPrint.Print();
}
private void mnuFilePrintPreview_Click(object sender, EventArgs e)
{
PrintDocument docPrint = new PrintDocument();
docPrint.PrintPage += new PrintPageEventHandler(docPrint_PrintPage);
if (PrinterSettings.InstalledPrinters.Count == 0)
{
ErrorMessage();
return;
}
objPreview.Document = docPrint;
objPreview.ShowDialog();
}
void ErrorMessage()
{
MessageBox.Show("No printers istalled. You must" + "have a printer installed to preview or print" + "the document.", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}