Ok.... Heres what I've got. I have written a program that queries an sql database displays it in a detailed listview, no problems so far. The only problem I am running in to is when I got to print the listview. When I do a regular printdocument.print() It prints out the listview exactly how I have it set up to do so. Which is a title , then column headers, next comes the listview, and then a total of some the columns. When I do a printpreview everything looks great. When I click the print button within the printpreviewdialog only the Title, Column headers, and the totals print.
I am completely lost and looking for some help.
Here is the print section of my code.
# private void TSMPageSetTotal_Click(object sender, System.EventArgs e)
{
System.Drawing.Printing.Margins newMargins;
newMargins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
PrintDocument1.DefaultPageSettings.Margins = newMargins;
PrintDocument1.DefaultPageSettings.Landscape = true;
{
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings;
PrintDocument1.DefaultPageSettings.Landscape = true;
}
try {
if (PageSetupDialog1.ShowDialog == DialogResult.OK) {
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings;
}
}
catch (Exception es) {
MessageBox.Show(es.Message);
}
}
private void TSMPrintTotal_Click(object sender, System.EventArgs e)
{
System.Drawing.Printing.Margins newMargins;
newMargins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
PrintDocument1.DefaultPageSettings.Margins = newMargins;
PrintDocument1.DefaultPageSettings.Landscape = true;
this.PrintDocument1.Print();
}
Font tableFont;
Font Font2;
int X1;
int X2;
int X3;
int X4;
int X5;
int X6;
int X7;
int X8;
int X9;
int X10;
int X11;
int X12;
int X13;
int X14;
int W1;
int W2;
int W3;
int W4;
int W5;
int W6;
int W7;
int W8;
int W9;
int W10;
int W11;
int W12;
int W13;
int Y;
int itm;
private void TSMPreviewTotal_Click(object sender, System.EventArgs e)
{
int pageWidth;
System.Drawing.Printing.Margins newMargins;
newMargins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
PrintDocument1.DefaultPageSettings.Margins = newMargins;
PrintDocument1.DefaultPageSettings.Landscape = true;
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings;
PageSetupDialog1.AllowOrientation = true;
if (PageSetupDialog1.ShowDialog() == Windows.Forms.DialogResult.OK) {
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings;
}
tableFont = new Font("Arial", 8);
Font2 = new Font("Arial", 10, FontStyle.Bold);
X1 = PrintDocument1.DefaultPageSettings.Margins.Left;
{
pageWidth = PrintDocument1.DefaultPageSettings.PaperSize.Width - PrintDocument1.DefaultPageSettings.Margins.Left - PrintDocument1.DefaultPageSettings.Margins.Right;
}
// X1, X2 and X3 are the X coordinates of the left edge of the three columns
// W1, W2 and W3 are the widths of the three columns
X1 = 25;
X2 = 80;
X3 = 140;
//CInt(X2 + pageWidth * 0.65)
X4 = 220;
X5 = 300;
X6 = 380;
X7 = 440;
X8 = 500;
X9 = 580;
X10 = 650;
X11 = 750;
X12 = 800;
X13 = 900;
X14 = 1000;
W1 = X2 - X1;
W2 = X3 - X2;
W3 = pageWidth - W1 - W2;
PrintPreviewDialog1.Document = PrintDocument1;
PrintPreviewDialog1.WindowState = FormWindowState.Maximized;
if (PrintPreviewDialog1.ShowDialog() == DialogResult.OK) {
this.PrintDocument1.Print();
}
itm = 0;
}
private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Y = PrintDocument1.DefaultPageSettings.Margins.Top + 60;
e.Graphics.DrawString("Total Report", new Font("Times New Roman", 36), Brushes.Red, 0, 0);
e.Graphics.DrawString("Ticket", Font2, Brushes.Black, X1, Y);
e.Graphics.DrawString("Team", Font2, Brushes.Black, X2, Y);
e.Graphics.DrawString("Order", Font2, Brushes.Black, X3, Y);
e.Graphics.DrawString("Part #", Font2, Brushes.Black, X4, Y);
e.Graphics.DrawString("Length", Font2, Brushes.Black, X5, Y);
e.Graphics.DrawString("Finish", Font2, Brushes.Black, X6, Y);
e.Graphics.DrawString("# of Ctn", Font2, Brushes.Black, X7, Y);
e.Graphics.DrawString("Pcs in Ctn", Font2, Brushes.Black, X8, Y);
e.Graphics.DrawString("Ctn Type", Font2, Brushes.Black, X9, Y);
e.Graphics.DrawString("Scrape Code", Font2, Brushes.Black, X10, Y);
e.Graphics.DrawString("Wt/Pc", Font2, Brushes.Black, X11, Y);
e.Graphics.DrawString("Lbs Packed", Font2, Brushes.Black, X12, Y);
e.Graphics.DrawString("TimeStamp", Font2, Brushes.Black, X13, Y);
Y = Y + 40;
while (itm < ListView1.Items.Count) {
string str;
str = ListView1.Items(itm).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y);
str = ListView1.Items(itm).SubItems(1).Text;
{
e.Graphics.DrawLine(Pens.Black, PrintDocument1.DefaultPageSettings.Margins.Left, Y, PrintDocument1.DefaultPageSettings.PaperSize.Height, Y);
}
e.Graphics.DrawString(str, tableFont, Brushes.Black, X2, Y);
str = ListView1.Items(itm).SubItems(2).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X3, Y);
str = ListView1.Items(itm).SubItems(3).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X4, Y);
str = ListView1.Items(itm).SubItems(4).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X5, Y);
str = ListView1.Items(itm).SubItems(5).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X6, Y);
str = ListView1.Items(itm).SubItems(6).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X7, Y);
str = ListView1.Items(itm).SubItems(7).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X8, Y);
str = ListView1.Items(itm).SubItems(8).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X9, Y);
str = ListView1.Items(itm).SubItems(9).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X10, Y);
str = ListView1.Items(itm).SubItems(10).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X11, Y);
str = ListView1.Items(itm).SubItems(11).Text;
e.Graphics.DrawString(str, tableFont, Brushes.Black, X12, Y);
str = ListView1.Items(itm).SubItems(12).Text;
RectangleF R = new RectangleF(X13, Y, 150, 150);
e.Graphics.DrawString(str, tableFont, Brushes.Black, R);
int lines;
int cols;
e.Graphics.MeasureString(str, tableFont, new SizeF(900, 150), new StringFormat(), cols, lines);
int Yc;
Yc = Y;
Yc = Yc + tableFont.Height + 2;
Y = Y + lines * tableFont.Height + 5;
Y = Math.Max(Y, Yc);
itm = itm + 1;
}
string totalPcs;
string totalCtn;
string totalLbs;
string totals;
totals = "Totals";
totalCtn = lblNumCtn.Text;
totalLbs = LblLbsPack.Text + " Lbs";
totalPcs = LblPcs.Text;
e.Graphics.DrawString(totals, Font2, Brushes.Black, X1, Y);
e.Graphics.DrawString(totalCtn, Font2, Brushes.Black, X7, Y);
e.Graphics.DrawString(totalPcs, Font2, Brushes.Black, X8, Y);
e.Graphics.DrawString(totalLbs, Font2, Brushes.Black, X12, Y);
{
e.Graphics.DrawLine(Pens.Black, PrintDocument1.DefaultPageSettings.Margins.Left, Y, PrintDocument1.DefaultPageSettings.PaperSize.Height, Y);
if (Y > 0.95 * (PrintDocument1.DefaultPageSettings.PaperSize.Width - PrintDocument1.DefaultPageSettings.Margins.Bottom)) {
e.HasMorePages = true;
return; // TODO: might not be correct. Was : Exit Sub
}
}
}

New Topic/Question
Reply




MultiQuote



|