无法将printviewdialog添加到Windows窗体

时间:2017-10-10 06:13:04

标签: c# wpf

我正在尝试创建一个Windows窗体应用程序,并且我无法在表单本身添加printviewdialog。下面的代码在显示表单之前弹出printviewdialog。

代码:

//Variables
PrintPreviewDialog printViewDialog = new PrintPreviewDialog();

public Form1()
{
   InitializeComponent();
   initComponents();
}

public void initComponents()
{
   printViewDialog.ClientSize = new System.Drawing.Size(500, 400);
   printViewDialog.Location = new System.Drawing.Point(0, 0);
   printViewDialog.ShowDialog();
   this.Controls.Add(printViewDialog);
}

收到错误:“this.Controls.Add(printViewDialog);”

错误:“无法将最高niveau元素添加到另一个元素。”

2 个答案:

答案 0 :(得分:0)

尝试使用

printViewDialog.TopLevel = false;
在添加之前

答案 1 :(得分:0)

我已经使用PrintPreviewControl来解决我的问题了,谢谢你们!

将PrintPreviewControl拖到我的表单中,并使用下面的代码进行一些设置:

//Get responsive width and height.
System.Drawing.Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
int height = workingRectangle.Height;
int width = workingRectangle.Width / 100 * 75;

//Settings printPreviewControl
printPreviewControl1.ClientSize = new System.Drawing.Size(width, height);
printPreviewControl1.Location = new System.Drawing.Point(0, 0);