Hi all
I have developed a litte Windows Program based on different Elements inter alia a Listbox called "LstAnzeige" to show Database Entrys on a list box. This works perfectly except the printing function. Which means, if I press on the Print Button the Windows Printer Dialog Window appears where i can choose the printer and the print the document respectivement the output of the Listbox but the sheets are all empty (see Attachments).
What's wrong?
Thanks for your help
I have developed a litte Windows Program based on different Elements inter alia a Listbox called "LstAnzeige" to show Database Entrys on a list box. This works perfectly except the printing function. Which means, if I press on the Print Button the Windows Printer Dialog Window appears where i can choose the printer and the print the document respectivement the output of the Listbox but the sheets are all empty (see Attachments).
What's wrong?
Thanks for your help
C#:
The Defenitions for the printer Function i put into the Form Load Method
public Form1()
{
InitializeComponent();
}
private StringReader myReader;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
LstAnzeige.Items.Add("" + i);
}
}
And this is the Code for the Printer Button
protected void CmdPrint_Click(object sender, EventArgs e)
{
// Printer Dialog Feld einblenden wo der User den Drucker auswählen und ggf. anpassen kann
// Und dann ListBox Einträge printen
PrintDialog.Document = PrintDocument;
string strText = "";
foreach (object x in LstAnzeige.Items)
{
strText = strText + x.ToString() + "\n";
}
myReader = new StringReader(strText);
if (PrintDialog.ShowDialog() == DialogResult.OK)
{
this.PrintDocument.Print();
}
}
protected void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.LstAnzeige.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
// Iterate over the string using the StringReader, printing each line.
while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont,
myBrush, leftMargin,
yPosition, new StringFormat());
count++;
// If there are more lines, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
myBrush.Dispose();
}
}
}