Print Receipt and Save Data Using Windows Forms in C#

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.   
  11. namespace PatientsAdmitReport  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         SqlDataAdapter da;  
  21.         DataSet ds;  
  22.         SqlConnection con;  
  23.   
  24.         private void button1_Click(object sender, EventArgs e)  
  25.         {  
  26.             con = new SqlConnection(@"DATA SOURCE=MyPC\SQLSERVER2K8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");  
  27.             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);  
  28.             ds = new DataSet();  
  29.             da.Fill(ds);  
  30.   
  31.             Form2 frm2 = new Form2();  
  32.             this.Hide();  
  33.             frm2.lblPatientName.Text = txtPatientName.Text.ToString();  
  34.             frm2.lblAge.Text = txtAge.Text.ToString();  
  35.             frm2.lblAddress.Text = txtAddress.Text.ToString();  
  36.             frm2.lblContactNo.Text = txtContactNo.Text.ToString();  
  37.             frm2.lblEmergencyContactNo.Text = txtEmergencyContactNo.Text.ToString();  
  38.             frm2.lblSex.Text = cmbSex.SelectedItem.ToString();  
  39.             frm2.Show();  
  40.         }  
  41.     }  
  42. }  
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.   
  11. namespace PatientsAdmitReport  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void print_Click(object sender, EventArgs e)  
  21.         {  
  22.             PrintScreen();  
  23.             printPreviewDialog1.ShowDialog();  
  24.         }  
  25.           
  26.         [System.Runtime.InteropServices.DllImport("gdi32.dll")]  
  27.         public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);  
  28.         private Bitmap memoryImage;  
  29.   
  30.         private void PrintScreen()  
  31.         {  
  32.             Graphics mygraphics = this.CreateGraphics();  
  33.             Size s = this.Size;  
  34.             memoryImage = new Bitmap(s.Width, s.Height, mygraphics);  
  35.             Graphics memoryGraphics = Graphics.FromImage(memoryImage);  
  36.             IntPtr dc1 = mygraphics.GetHdc();  
  37.             IntPtr dc2 = memoryGraphics.GetHdc();  
  38.             BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);  
  39.             mygraphics.ReleaseHdc(dc1);  
  40.             memoryGraphics.ReleaseHdc(dc2);  
  41.         }  
  42.   
  43.         private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)  
  44.         {  
  45.             e.Graphics.DrawImage(memoryImage, 0, 0);  
  46.         }         
  47.     }  
  48. }  
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


Similar Articles