[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

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?
0
brenlex
Asked:
brenlex
  • 2
1 Solution
 
Nirmalan NagenthiranCommented:
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

0
 
brenlexAuthor Commented:
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.
0
 
Nirmalan NagenthiranCommented:
Ty this way and see.
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 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!
}

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now