检查表单上的所有元素是否已完全加载C#

时间:2016-04-03 16:16:54

标签: c# visual-studio-2015

我正在使用Visual Studio 2015开发一个C#应用程序 它有2个表单,在form1上我有一个点击按钮 显示form2,现在我想做的是打印form2 完全加载后,我正在使用printform控件 在form2上执行此操作,如果我在form_load事件上使用它 它打印一个空白页然后显示表单,我有 也尝试在form_Shown上使用它,但是这会打印一个框 元素本身但不是元素本身,就像它们一样 没有完成加载,可能有更好的方法来做到这一点 但我是C#的新手,所以还在学习

以下是我在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();
        }
    }
}

1 个答案:

答案 0 :(得分:4)

显示的事件过早发射。表格的框架和背景是绘制的,但其余部分后面。绘画是一项低优先级的任务,只有在不需要其他任何事情时才会发生,首先发生Shown事件。

解决方法很简单,只需要求表单通过调用Update()方法完成更新。修正:

    private void Form2_Shown(object sender, EventArgs e)
    {
        this.Update();
        printForm.Print();
    }