Zebra0.com

csharp listbox2


Text of video

Write the code as shown below:

// Programmer: Janet Joy
// A simple printing application

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.IO;
using System.Drawing.Printing;

namespace print
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Make sure the font dialog has a font assigned to it.
            fontDialog1.Font = this.Font;
        }
        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // The font from fontDialog is used for the print document.
            // We don't care if they cancel because we asign a font in form load.
            fontDialog1.ShowDialog();
            listBox1.Font = fontDialog1.Font;
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // Set up the document for printing: we will write to the documents graphics.
            Graphics page = e.Graphics;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            // This assumes that the contents of the list box will fit on one page.
            int xpos = 50; //50 pixel margin on left
            int ypos = 80; //80 pixel margin at top
            foreach (string item in listBox1.Items)
            {
                // Draw each item in the list box onto the documents graphics.http://www.zebra0.com/csharp/index.php#lesson19
                page.DrawString(item, fontDialog1.Font, myBrush, xpos, ypos);
                // Move down the y position before drawing the next line.
                ypos = ypos + fontDialog1.Font.Height+ 3;  
            }
        }
        
        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Print dialog lets user select a print, number of copies, start page, etc.
            // Allows the user to select current selection.         
            printDialog1.AllowSelection = true;
            // Allows user to select current page only.
            printDialog1.AllowCurrentPage = true;
            // Allows user to select a range of pages.
            printDialog1.AllowSomePages = true;

            // To keep it simple we have ignored all of these settings
            // and assume the items in list box will fit on one page .
            DialogResult result = printDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Assign all of the settings from the print dialog to the document.
                printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                // Calling print executes the code in printDocument1_PrintPage
                printDocument1.Print();
            }

        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Assign printDocument1 as the preview document. 
            printPreviewDialog1.Document = printDocument1;
            // The print preview dialog shows the document.
            printPreviewDialog1.ShowDialog();
        }
    }
}

To Do:Make sure everything works!

End of lesson, Next lesson: Object oriented programming in C#