Printer does nohting

elais12

Member
Joined
Jul 20, 2018
Messages
7
Programming Experience
Beginner
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
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();
        }

        }
}
 

Attachments

  • Drucken_2018-08-19_21-46-49.png
    Drucken_2018-08-19_21-46-49.png
    15 KB · Views: 80
  • PrintMessage.png
    PrintMessage.png
    1.8 KB · Views: 82
  • document.pdf
    5.1 KB · Views: 81
When you debugged your code, i.e. set a breakpoint and stepped through the code line by line and examined the state at each step, where and how exactly did the behaviour differ from your expectation? That's pretty much a rhetorical question because I think that it's safe to say that you didn't debug your code because, if you had the issue have been fairly obvious. Stop what you're doing and learn how to debug NOW. It is a critical skill that every developer should learn before tackling anything more complex than "hello world".

As for the specific issue, take a look at this part of your code:
            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++;

That's the code as you have it written. That is effectively this:
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++;

Because you have not included any braces at all, only the first line immediately after the 'while' statement is part of the loop. That means that the loop just keeps on setting 'yPosition' until 'line' is equal to 'null'. At that point it exits the loop and draws 'line', which is 'null', which you can't see on a printed page.

Like I said, if you had debugged the code then you'd have seen what was happening and the issue would have been obvious. The code needs to be written like this:
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++;
}
 
You seem to be quite sloppy with your braces and that is likely at least part of the problem. There are where you have one-line blocks and you have used braces, e.g.
            foreach (object x in LstAnzeige.Items)
            {
                strText = strText + x.ToString() + "\n";
            }
            myReader = new StringReader(strText);
            if (PrintDialog.ShowDialog() == DialogResult.OK)
            {
                this.PrintDocument.Print();
            }

and other places where you haven't, e.g.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;

Inconsistency is ALWAYS bad. No matter what aspect of your coding it is, pick a style and stick to it. Personally, I ALWAYS wrap a block in braces, whether it's one line or more. That way, I can NEVER do what you did here and forget to add braces where they are required. If you're going to not use braces on one-line blocks then commit to doing so EVERY time and also indent your code properly so it is clear when a line is part of a one-line block and when it's not. That way you won't make mistakes like this either because it would be clear that the code that isn't indented is not part of the block and code that is indented must be part of a block and therefore requires braces.
 
THanks for your reply. I will try. I debugged truley and truley but it din't realized that the loop (i set exactly there a BP) will be always null..

And it works now thanks again for your help
 
In that case, you need to work on your debugging skills. You should have seen that there was only one line in the loop and that the DrawString line was executed only once and that 'line' was null at that point. Maybe not as a first step but certainly before you posted here you should have placed a breakpoint on the first line of the method, stepped through every line of the method and watched every variable in the method. I don't know how you could do that and not see that DrawString was called only once and was drawing null, yet you didn't tell us that that was what was happening.
 
Back
Top Bottom