Skip to content

Printing a foreach in c# Forms

cory thompson

Asked Oct 5, 2012, 6:00 AM EDT1 answer
Share
LinkedInX
I am trying to print a page for each item in my listview.

TypeScript
        private void btnPrintForm_Click(object sender, EventArgs e)
        {
            printDocument2.Print();
        }
        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics info = e.Graphics;
            String infomessage = DateTime.Today.ToString("d") + "\r\n" + listView1.Items[0].SubItems[4].Text;
   Font infoFont = new Font("Arial", 12, System.Drawing.GraphicsUnit.Point);
   info.DrawString(infomessage, infoFont, Brushes.Black, 250, 200);
  }


My above code shows printing of my listview,
What i am trying to print is a new page for each listview row.

My failed attempts so far frint the same info on multiple sheets and different infor over the same sheet.

Thanks in advance.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    TypeScript
            private void btnPrintForm_Click(object sender, EventArgs e)
            {
                printDocument2.Print();
            }
            private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
            {
              foreach (ListViewItem lvi in listview1)
                {
                Graphics info = e.Graphics;
                String infomessage = DateTime.Today.ToString("d") + "\r\n" +                        listView1.Items[0].SubItems[4].Text;
               Font infoFont = new Font("Arial", 12, System.Drawing.GraphicsUnit.Point);
               info.DrawString(infomessage, infoFont, Brushes.Black, 250, 200);

        }

    }

    This does not work. This will print each row over the top of the last on the same page.

    0