Windows Forms: Print Windows Form in C#
By FoxLearn 7/2/2017 9:22:52 PM 10.14K
How to print windows forms in c# using PrintDocument, PrintPreviewDialog
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintWindowsForms" and then click OK
Step 2: Design your form as below
We use the Northwind database to play demo. If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server
Step 3: Create an EF Model as below
Step 4: Add code to handle your forms as below
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 PrintWindowsForms { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //Draw form e.Graphics.DrawImage(bmp, 0, 0); } Bitmap bmp; private void btnPrint_Click(object sender, EventArgs e) { //Open print preview dialog Graphics g = this.CreateGraphics(); bmp = new Bitmap(this.Size.Width, this.Size.Height, g); Graphics mg = Graphics.FromImage(bmp); mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size); printPreviewDialog1.ShowDialog(); } private void Form1_Load(object sender, EventArgs e) { //Get product data using(NorthWindEntities db = new NorthWindEntities()) { productBindingSource.DataSource = db.Products.ToList(); } } } }
VIDEO TUTORIALS
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: Text to speech in C#
- Windows Forms: Print Picture Box in C#
- Windows Forms: Advanced Filter DataGridView in C#
- Windows Forms: Progress Bar in C#
- Windows Forms: How to Create a Random Password Generator in C#
- Windows Forms: Tag Control in C#
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024