Link to home
Start Free TrialLog in
Avatar of brenlex
brenlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Flicker on main form when using PrintDialog

From within my C# app, when I launch a print preview dialog using ShowDialog from a button click on my main form, my main form (behind the preview dialog) flickers whilst the preview job is being built.  The print document is built from the contents of a listbox using the code below.

        private void buttonPrint_Click(object sender, EventArgs e)
        {
                System.Drawing.Printing.PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                printPreviewDialog1.Document = pd;
                printPreviewDialog1.ShowDialog();
        }
       
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            Font = new Font("Courier New", 9);
            linesPerPage = ev.MarginBounds.Height / Font.GetHeight(ev.Graphics);
            while(count < linesPerPage && nextItem < listBoxResults.Items.Count)
            {
                yPos = topMargin + (count * Font.GetHeight(ev.Graphics));
                line = listBoxResults.Items[nextItem].ToString();
                ev.Graphics.DrawString(line, Font, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
                nextItem++;
               
                if (nextItem <= listBoxResults.Items.Count - 1)
                    ev.HasMorePages = true;
                else
                {
                    ev.HasMorePages = false;
                    nextItem = 0;
                    break;
                }
            }
            Font = new Font("Microsoft Sans Serif", 8);             // Set our font back on main form!
        }

How can I stop the flicker on the main form, as it seems the pd_PrintPage event is being fired by the ShowDialog() method itself?
Avatar of Nirmalan Nagenthiran
Nirmalan Nagenthiran
Flag of Australia image

Just comment out the
Just comment out the Font related stuff in pd_PrintPage event handler
It looks ok.
 
//Font = new Font("Courier New", 9);
 
 
//Font = new Font("Microsoft Sans Serif", 8);       

Open in new window

Avatar of brenlex

ASKER

In which case is there any other way to change the preview / printed font to Courier New? I need this as the print jobs' text columns are not aligned otherwise.
ASKER CERTIFIED SOLUTION
Avatar of Nirmalan Nagenthiran
Nirmalan Nagenthiran
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial