'How can i print a listbox list in c# windows form app?

I'm currently having issues with my code in terms of getting a printing option to show up when thy click a specific button.

I am making a reminder program and was just experimenting. Also would like to know the most efficient way to add a daily notification system to my program

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Drawing.Printing;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace simpleapp
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("Reminder: " + textBox1.Text);


        }
        private void input_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                listBox1.Items.Add("Reminder: " + textBox1.Text  );
            }

        } 

        private void button2_Click(object sender, EventArgs e)
        {
            for (int rmd = listBox1.SelectedIndices.Count - 1; rmd >= 0; rmd--)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndices[rmd]);

            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {


            listBox1.SelectionMode = SelectionMode.MultiExtended;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            FAQ faqs = new FAQ();
            faqs.Show();

        }

        // When the Button is Clicked the List is saved in a ".txt file format for the user to view later 
        private void button3_Click(object sender, EventArgs e)
        {
            var Savingfileas = new SaveFileDialog();
            Savingfileas.Filter = "Text (*.txt)|*.txt ";
            if (Savingfileas.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var Reminder = new StreamWriter(Savingfileas.FileName, false))
                    foreach (var item in listBox1.Items)
                        Reminder.Write(item.ToString() + Environment.NewLine);
                MessageBox.Show("File has been successfully saved"+ '\n' + "Thank you for using the Remindr program");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            Email_Client emc = new Email_Client();
            emc.Show();



        }


    }
}


Solution 1:[1]

You have a few options. One is to graphically layout a print document and iterate through your listbox while drawing the text in the textbox using x and y coordinates. A better way would be to get your listbox items into a dataset and use it to generate a report. Are you storing these items in a database for retrieval? Here's a link that should help you get started. The tutorial is in VB.Net but there's only a small amount of code to this and should be easy to repeat using C# code.

Solution 2:[2]

Here is an absolutely minimal example of printing a ListBox.

It assumes your ListBox contains strings and shows a PrintPreviewDialog; it sets the page unit to mm, do pick a unit you are comfortable with..!

Of course you may chose one or more different fonts etc..

    private PrintDocument document =   new PrintDocument();

    private void printButton_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog ppd = new PrintPreviewDialog();
        ppd.Document = document;
        ppd.Document.DocumentName = "TESTING";
        document.PrintPage += document_PrintPage;
        ppd.ShowDialog();
    }

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        int leading = 5;
        int leftMargin = 25;
        int topMargin = 10;

        // a few simple formatting options..
        StringFormat FmtRight = new StringFormat() { Alignment = StringAlignment.Far};
        StringFormat FmtLeft = new StringFormat() { Alignment = StringAlignment.Near};
        StringFormat FmtCenter = new StringFormat() { Alignment = StringAlignment.Near};

        StringFormat fmt = FmtRight;

        using (Font font = new Font( "Arial Narrow", 12f))
        {
        SizeF sz = e.Graphics.MeasureString("_|", Font);
        float h = sz.Height + leading;

        for (int i = 0; i < listBox1.Items.Count; i++)
            e.Graphics.DrawString(listBox1.Items[i].ToString(), font , Brushes.Black, 
                                  leftMargin, topMargin + h * i, fmt);
        }
    }

The actual printing is triggered when the user clicks the printer symbol in the dialog.

Note that there are more StringFormat options!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charles May
Solution 2