This article shows how to print from a Windows Forms form. Here in this example I am using a Patient Detail form. I am inserting data into a SQL Server Data Table and printing the slip.

The following is my Form1.cs.

patient details
Image 1

On the Submit button click I am opening another form and inserting this form data into SQL Server.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. namespace PatientsAdmitReport
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. SqlDataAdapter da;
  19. DataSet ds;
  20. SqlConnection con;
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. con = new SqlConnection(@"DATA SOURCE=MyPC\SQLSERVER2K8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");
  24. da = new SqlDataAdapter("INSERT INTO Patient(PatientName,Age, Address,ContactNo, EmergencyContactNo, Sex) VALUES('" + txtPatientName.Text + "','" + txtAge.Text + "','" + txtAddress.Text + "','" + txtContactNo.Text + "','" + txtEmergencyContactNo.Text + "','" + cmbSex.SelectedItem.ToString() + "')", con);
  25. ds = new DataSet();
  26. da.Fill(ds);
  27. Form2 frm2 = new Form2();
  28. this.Hide();
  29. frm2.lblPatientName.Text = txtPatientName.Text.ToString();
  30. frm2.lblAge.Text = txtAge.Text.ToString();
  31. frm2.lblAddress.Text = txtAddress.Text.ToString();
  32. frm2.lblContactNo.Text = txtContactNo.Text.ToString();
  33. frm2.lblEmergencyContactNo.Text = txtEmergencyContactNo.Text.ToString();
  34. frm2.lblSex.Text = cmbSex.SelectedItem.ToString();
  35. frm2.Show();
  36. }
  37. }
  38. }
The following is my form2. Here you can design this form as you want for your receipt. Here I am using a TableLayOut Panel and some label. I also add a PrintPreviewControl and PrintPreviewDialog on this form2.

print preview
Image 2

Now, I am showing my data in label7, label8, label9 …… label12. As you can see I am using these labels on form1. So I need to change the Modifier property of these labels like the following.

properties
Image 3

Now, right-click on printPreviewDialog1 and select Properties and set the Document Properties to printDocument1 like the following.

prinPreview Dialog
Image 4

Now, right-click on printDocument1 and select Properties in the set PrintPage event like the following.

print page
Image 5

Now, the code of form 2 is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Printing;
  10. namespace PatientsAdmitReport
  11. {
  12. public partial class Form2 : Form
  13. {
  14. public Form2()
  15. {
  16. InitializeComponent();
  17. }
  18. private void print_Click(object sender, EventArgs e)
  19. {
  20. PrintScreen();
  21. printPreviewDialog1.ShowDialog();
  22. }
  23. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  24. public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
  25. private Bitmap memoryImage;
  26. private void PrintScreen()
  27. {
  28. Graphics mygraphics = this.CreateGraphics();
  29. Size s = this.Size;
  30. memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
  31. Graphics memoryGraphics = Graphics.FromImage(memoryImage);
  32. IntPtr dc1 = mygraphics.GetHdc();
  33. IntPtr dc2 = memoryGraphics.GetHdc();
  34. BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
  35. mygraphics.ReleaseHdc(dc1);
  36. memoryGraphics.ReleaseHdc(dc2);
  37. }
  38. private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  39. {
  40. e.Graphics.DrawImage(memoryImage, 0, 0);
  41. }
  42. }
  43. }
Now, run the application:

patient detail
Image 6

Here fill in the patient details and click on Submit.

form 2
Image 7

This is the slip here. Click on the Print button.

page print preview
Image 8

From here by clicking on the Print option you can print the slip. You can also increase the size of the slip.

Now, see the following in the database:

server management
Image 9

The following is my table design.

design view
Image 10