如何从C#中的其他表单打印Windows窗体 [英] How can I print a windows form from another form in C#

查看:119
本文介绍了如何从C#中的其他表单打印Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个包含所有标签的表单,这些标签将从另一个窗体的数据网格的选定值中填充,并希望自动打印而不显示它。我在窗体上设置printDocument和printDialog并使用Form上的按钮进行打印然后它正常工作但是当我将所有控件公开后调用另一个窗体的button1_Click方法作为button1_Click(null,null)时我得到黑页作为预览。



我在Form1中有一个gridview,在Form1中有一个button1。 Form2的排列方式与发票的外观类似。想要在填充Form1的datagrid值并单击button1后打印Form2。

解决方案

首先,重要的是要意识到表单设计为在屏幕上显示,不打印;和打印机,如果设备非常不同。你无法在打印页面上调整大小,滚动,缩放或平移,点击控件,向下钻取......所以,在大多数情况下,最好不要使用类<$ c来打印表单,而只打印表单上显示的数据$ c> PrintDialog :

http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



尽管如此,表格的简化打印仍然很受欢迎,因此您可以看到,例如,这些CodeProject文章中的一些:

FormPrint作为一个简单的类 [ ^ ],

使用API​​打印Windows窗体 [ ^ ],

表格打印控制 [ ^ ],

用C#简化.NET打印 [ ^ ],

打印任何控件的组件,包括ListViews,TreeViews,DataGridViews和Forms [ ^ ],

以报告方式打印表格(2.2版) [ ^ ]。



我希望你会看到从另一种形式打印的问题在这里无关紧要。如果您不这样做,请理解这是形式协作这个流行问题的一个非常基本和基本的问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。

新年快乐!



-SA


除了解决方案1,与形式合作有关:



因为这个问题非常受欢迎,我以前的答案往往不是很好理解,可能不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:许多问题一次回答 - Windows窗体或WPF Windows之间的协作



-SA

I have designed a form with all labels that will be populated from the selected value of a datagrid of another window form and want to automatically print it without showing it. I set printDocument and printDialog on the form and use a button on the Form to print then it works properly but when I call button1_Click method of that form from another form as button1_Click(null, null) after making all controls public I am getting black page as a preview.

I have a gridview in Form1 and button1 in Form1. Form2 is arranged with levels how the invoice looks like. Want to print Form2 after populating with values of datagrid of Form1 and clicking button1.

解决方案

First of all, it's important to realize that forms are designed to be shown on screen, not printed; and the printer if very different device. You cannot resize, scroll, zoom or pan on a printed page, click on controls, drill down… So, in most cases, it's better to print not the form, but just the data presented on a form, using the class PrintDialog:
http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog%28v=vs.110%29.aspx[^].

Still, simplified printing of a form is on high demand, so you can see, for example, some of these CodeProject articles:
FormPrint as a simple class[^],
Print Windows Forms w/o using API[^],
Form Print Control[^],
Simplified .NET Printing in C#[^],
A component that prints any control, including ListViews, TreeViews, DataGridViews, and Forms[^],
Printing a Form in a report fashion (release 2.2)[^].

I hope you will see that the issue of "printing from another form" is irrelevant here. In case you won't, please understand that this is a very basic and elementary issue of form collaboration, popular question. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

Happy New Year!

—SA


In addition to Solution 1, related to "form collaboration":

As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于如何从C#中的其他表单打印Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆