Reputation: 784
I am developing a C# application using Visual Studio 2015 it has 2 forms, on form1 I have a button that when clicked shows form2, now what I would like to do is print form2 after it has fully loaded, I am using the printform control on form2 to do this, if I use this on the the form_load event it prints a blank page and then shows the form, I have also tried using it on form_Shown, however this prints a box where the elements are but not the element itself as if they have not finished loading, there is probably a better way to do this but I am new to C# so still learning
Below is an example of the code that I have on form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyApp
{
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.Shown += new System.EventHandler(this.Form2_Shown);
}
private void Form2_Shown(object sender, EventArgs e)
{
printForm.Print();
}
}
}
Upvotes: 2
Views: 1576
Reputation: 941465
The Shown event fires a wee bit too early. The form's frame and background is painted but the rest follows later. Painting is a low priority task that occurs only when nothing else needs to be done, firing the Shown event happens first.
The workaround is simple, just ask the form to finish updating itself by calling its Update() method. Fix:
private void Form2_Shown(object sender, EventArgs e)
{
this.Update();
printForm.Print();
}
Upvotes: 6