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.
Image 1
On the Submit button click I am opening another form and inserting this form data into SQL Server.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
-
- namespace PatientsAdmitReport
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- SqlDataAdapter da;
- DataSet ds;
- SqlConnection con;
-
- private void button1_Click(object sender, EventArgs e)
- {
- con = new SqlConnection(@"DATA SOURCE=MyPC\SQLSERVER2K8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");
- 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);
- ds = new DataSet();
- da.Fill(ds);
-
- Form2 frm2 = new Form2();
- this.Hide();
- frm2.lblPatientName.Text = txtPatientName.Text.ToString();
- frm2.lblAge.Text = txtAge.Text.ToString();
- frm2.lblAddress.Text = txtAddress.Text.ToString();
- frm2.lblContactNo.Text = txtContactNo.Text.ToString();
- frm2.lblEmergencyContactNo.Text = txtEmergencyContactNo.Text.ToString();
- frm2.lblSex.Text = cmbSex.SelectedItem.ToString();
- frm2.Show();
- }
- }
- }
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.
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.
Image 3
Now, right-click on printPreviewDialog1 and select Properties and set the Document Properties to printDocument1 like the following.
Image 4
Now, right-click on printDocument1 and select Properties in the set PrintPage event like the following.
Image 5
Now, the code of form 2 is:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Printing;
-
- namespace PatientsAdmitReport
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- private void print_Click(object sender, EventArgs e)
- {
- PrintScreen();
- printPreviewDialog1.ShowDialog();
- }
-
- [System.Runtime.InteropServices.DllImport("gdi32.dll")]
- public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
- private Bitmap memoryImage;
-
- private void PrintScreen()
- {
- Graphics mygraphics = this.CreateGraphics();
- Size s = this.Size;
- memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
- Graphics memoryGraphics = Graphics.FromImage(memoryImage);
- IntPtr dc1 = mygraphics.GetHdc();
- IntPtr dc2 = memoryGraphics.GetHdc();
- BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
- mygraphics.ReleaseHdc(dc1);
- memoryGraphics.ReleaseHdc(dc2);
- }
-
- private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
- {
- e.Graphics.DrawImage(memoryImage, 0, 0);
- }
- }
- }
Now, run the application:
Image 6
Here fill in the patient details and click on Submit.
Image 7
This is the slip here. Click on the Print button.
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:
Image 9
The following is my table design.
Image 10