In Visual Studio C# Winforms please help me understand how to print the list of items in a combobox and/or listbox. I know a loop needs to be created to process through the list then output that list to a printer but so far I have not been able to make it work. I can print if I select one item from the list. I cannot get the entire list to print to a printer.
Thanks in advance,
Rashad
Loading
FroglegPosted Feb 20, 2013, 3:05 PM
namespace ListPrint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Font myFont = new Font("Arial", 16);
private void Form1_Load(object sender, EventArgs e)
{
loadList();
}
public void loadList()
{
listBox1.Items.Add("Dog");
listBox1.Items.Add("Cat");
listBox1.Items.Add("Horse");
listBox1.Items.Add("Cow");
}
private void bttnPreview_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog();
}
private void bttnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 20, y = 20,i = 0;
foreach (string str in listBox1.Items)
{
e.Graphics.DrawString(str, myFont, Brushes.Black, x, y);
y += 25;
i++;
}
}
}
}