Print Prescription in C# and Visual Basic

Introduction

This is a trial to print a Prescription using three controls: PrintPreviewDialog control, PrintDocument control and PrintDialog control.

print

My Project has one form (Form1) and two classes (clsReport and Globals).

Form1: has three text boxes for entering the data of the patient, the patient number and visit date.

Another three text boxes for entering the medication.

Three controls for printing: PrintPreviewDialog, PrintDocument and PrintDialog.

clsReport: has procedures for printing.

Globals: for some variables.

clsReport has the following three procedures:
  1. public void ReadReport()  
  2.         {  
  3.             try  
  4.             {  
  5.         // read data from Form1  
  6.         // you can get the data from any data file  
  7.   
  8.                 RepVisitDate = "Date" + ": " + Globals.VisitDate;  
  9.                 RepSickName = "Name" + ": " + Globals.illName;  
  10.                 RepSickNum = "File number" + ": " + Globals.illNum;  
  11.   
  12.                 Medicine1 = Globals.FirstMidc;  
  13.                 Medicine2 = Globals.SecondMidc;  
  14.                 Medicine3 = Globals.ThirdMidc;  
  15.             }  
  16.             catch  
  17.             {  
  18.                 MessageBox.Show("Reading Error.");  
  19.             }  
  20.   
  21.         private void ComputeLeftAll(Graphics g)  
  22.         {  
  23.             //compute the left location of all lines for,  
  24.             //Recipe head, Sick data and Medicine.  
  25.   
  26.             Marginleft = Globals.leftMargin;  
  27.             Marginright = Globals.rightMargin;  
  28.             RepWid = Globals.ReportWid;  
  29.   
  30.             int lenWork = (int)g.MeasureString(dcWork, ArialB14).Width;  
  31.             int lenDocHead1 = (int)g.MeasureString(DocHead1, ArialB18).Width;  
  32.             int lenDocHead2 = (int)g.MeasureString(DocHead2, ArialB16).Width;  
  33.   
  34.             int lenSickVisit = (int)g.MeasureString(RepVisitDate, ArialB14).Width;  
  35.             int lenSickName = (int)g.MeasureString(RepSickName, ArialB14).Width;  
  36.             int lenSickNum = (int)g.MeasureString(RepSickNum, ArialB14).Width;  
  37.   
  38.             int lenRepTail1 = (int)g.MeasureString(RepTail1, ArialB14).Width;  
  39.   
  40.             xWork = Marginleft + (RepWid - lenWork) / 2;  
  41.             xDocHead1 = Marginleft + (RepWid - lenDocHead1) / 2;  
  42.             xDocHead2 = Marginleft + (RepWid - lenDocHead2) / 2;  
  43.   
  44.             xSickName = Marginleft;   
  45.             xSickNum = Marginleft + (Marginright - Marginleft - lenSickNum) / 2;  
  46.             xSickVisit = Marginright - lenSickName;  
  47.   
  48.             xRepTail1 = Marginleft + (Marginright - Marginleft - lenRepTail1) / 2;  
  49.         }  
  50.   
  51.         public void DrawReport(Graphics g)  
  52.         {  
  53.             int CurrentY = Globals.topMargin;  
  54.             int CurrentX = Globals.leftMargin;  
  55.   
  56.             try  
  57.             {  
  58.                 // for Recipe  
  59.                 ArialR14 = new Font("Arial", 14, FontStyle.Regular);  
  60.                 int ArialRH14 = (int)(ArialR14.GetHeight(g));  
  61.                 // for EyeHorus  
  62.                 Font CorsivaFont = new Font("Monotype Corsiva", 18, FontStyle.Italic);  
  63.                 int CorsivaHeight = (int)(CorsivaFont.GetHeight(g));  
  64.                 // for Name  
  65.                 ArialB14 = new Font("Arial", 14, FontStyle.Bold);  
  66.                 int ArialBH14 = (int)(ArialB14.GetHeight(g));  
  67.                 ArialB16 = new Font("Arial", 16, FontStyle.Bold);  
  68.                 int ArialBH16 = (int)(ArialB16.GetHeight(g));  
  69.                 ArialB18 = new Font("Arial", 18, FontStyle.Bold);  
  70.                 int ArialBH18 = (int)(ArialB18.GetHeight(g));  
  71.                 ArialB20 = new Font("Arial", 20, FontStyle.Bold);  
  72.                 int ArialBH20 = (int)(ArialB20.GetHeight(g));  
  73.   
  74.                 ComputeLeftAll(g);  
  75.   
  76.                 SolidBrush myBrush = new SolidBrush(Color.Black);  
  77.   
  78.                 CurrentY = CurrentY + ArialRH14;  
  79.                 //Draw Report Head:  
  80.                 g.DrawString(dcWork, ArialB14, myBrush, xWork, CurrentY); //dcWorkAR  
  81.                 CurrentY = CurrentY + ArialBH14;  
  82.                 g.DrawString(DocHead1, ArialB18, myBrush, xDocHead1, CurrentY); //DocHeadAR1  
  83.                 CurrentY = CurrentY + ArialBH20;  
  84.                 g.DrawString(DocHead2, ArialB16, myBrush, xDocHead2, CurrentY); //DocHeadAR2  
  85.                 CurrentY = CurrentY + 2 * ArialBH14;  
  86.                 // Draw line  
  87.                 g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);  
  88.                 CurrentY = CurrentY + ArialRH14;  
  89.                 // Draw Personal Data:  
  90.                 g.DrawString(RepSickName, ArialB14, myBrush, xSickName, CurrentY);  
  91.                 g.DrawString(RepSickNum, ArialB14, myBrush, xSickNum, CurrentY);  
  92.                 g.DrawString(RepVisitDate, ArialB14, myBrush, xSickVisit, CurrentY);  
  93.                 CurrentY = CurrentY + 2 * ArialBH14;  
  94.                 // Draw EyeHorus:  
  95.                 g.DrawString(EyeHorus, CorsivaFont, myBrush, CurrentX, CurrentY);  
  96.                 CurrentY = CurrentY + CorsivaHeight;  
  97.                 // Draw Recipe:  
  98.                 CurrentY = CurrentY + ArialRH14;  
  99.                 g.DrawString(Medicine1, ArialR14, myBrush, CurrentX, CurrentY);  
  100.                 CurrentY = CurrentY + ArialRH14;  
  101.                 g.DrawString(Medicine2, ArialR14, myBrush, CurrentX, CurrentY);  
  102.                 CurrentY = CurrentY + ArialRH14;  
  103.                 g.DrawString(Medicine3, ArialR14, myBrush, CurrentX, CurrentY);  
  104.                 // Go to end of page:  
  105.                 CurrentY = Globals.bottomMargin - 4 * ArialRH14;  
  106.                 // Draw line  
  107.                 g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);  
  108.                 // Draw Report Tail:  
  109.                 CurrentY = CurrentY + ArialRH14;  
  110.                 g.DrawString(RepTail1, ArialB14, myBrush, xRepTail1, CurrentY); //RepTail1  
  111.   
  112.                 ArialB14.Dispose();  
  113.                 ArialB16.Dispose();  
  114.                 ArialB18.Dispose();  
  115.                 ArialB20.Dispose();  
  116.                 ArialR14.Dispose();  
  117.                 CorsivaFont.Dispose();  
  118.                 g.Dispose();  
  119.             }  
  120.             catch  
  121.             {  
  122.                 string Msg = "Can't print, no Font!";  
  123.                 MessageBox.Show(Msg, "Printing.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  
  124.             }  
  125.         } 

Using VB to print Prescription

  1. Public Sub ReadReport()  
  2.         Try  
  3.     ' read data from Form1  
  4.     ' you can get the data from any data file  
  5.   
  6.             RepVisitDate = "Date" + ": " + VisitDate  
  7.             RepSickName = "Name" + ": " + illName  
  8.             RepSickNum = "File number" + ": " + illNum  
  9.   
  10.             Medicine1 = FirstMidc  
  11.             Medicine2 = SecondMidc  
  12.             Medicine3 = ThirdMidc  
  13.         Catch  
  14.             MessageBox.Show("Reading Error.")  
  15.         End Try  
  16.     End Sub  
  17.   
  18.     Private Sub ComputeLeftAll(ByVal g As Graphics)  
  19.         'compute the left location of all lines for,  
  20.         'Recipe head, Sick data and Medicine.  
  21.   
  22.         Dim lenWork As Integer = Convert.ToInt32(g.MeasureString(dcWork, ArialB14).Width)  
  23.         Dim lenDocHead1 As Integer = Convert.ToInt32(g.MeasureString(DocHead1, ArialB18).Width)  
  24.         Dim lenDocHead2 As Integer = Convert.ToInt32(g.MeasureString(DocHead2, ArialB16).Width)  
  25.   
  26.         Dim lenSickVisit As Integer = Convert.ToInt32(g.MeasureString(RepVisitDate, ArialB14).Width)  
  27.         Dim lenSickName As Integer = Convert.ToInt32(g.MeasureString(RepSickName, ArialB14).Width)  
  28.         Dim lenSickNum As Integer = Convert.ToInt32(g.MeasureString(RepSickNum, ArialB14).Width)  
  29.   
  30.         Dim lenRepTail1 As Integer = Convert.ToInt32(g.MeasureString(RepTail1, ArialB14).Width)  
  31.   
  32.         xWork = leftMargin + (ReportWid - lenWork) / 2  
  33.         xDocHead1 = leftMargin + (ReportWid - lenDocHead1) / 2  
  34.         xDocHead2 = leftMargin + (ReportWid - lenDocHead2) / 2  
  35.   
  36.         xSickName = leftMargin  
  37.         xSickNum = leftMargin + (rightMargin - leftMargin - lenSickNum) / 2  
  38.         xSickVisit = rightMargin - lenSickName  
  39.   
  40.         xRepTail1 = leftMargin + (rightMargin - leftMargin - lenRepTail1) / 2  
  41.     End Sub  
  42.   
  43.     Public Sub DrawReport(ByVal g As Graphics)  
  44.         Dim CurrentY As Integer = topMargin  
  45.         Dim CurrentX As Integer = leftMargin  
  46.   
  47.         Try  
  48.             ' for Recipe  
  49.             ArialR14 = New Font("Arial", 14, FontStyle.Regular)  
  50.             Dim ArialRH14 As Integer = Convert.ToInt32(ArialR14.GetHeight(g))  
  51.             ' for EyeHorus  
  52.             Dim CorsivaFont As Font = New Font("Monotype Corsiva", 18, FontStyle.Italic)  
  53.             Dim CorsivaHeight As Integer = Convert.ToInt32(CorsivaFont.GetHeight(g))  
  54.             ' for Name  
  55.             ArialB14 = New Font("Arial", 14, FontStyle.Bold)  
  56.             Dim ArialBH14 = Convert.ToInt32(ArialB14.GetHeight(g))  
  57.             ArialB16 = New Font("Arial", 16, FontStyle.Bold)  
  58.             Dim ArialBH16 As Integer = Convert.ToInt32(ArialB16.GetHeight(g))  
  59.             ArialB18 = New Font("Arial", 18, FontStyle.Bold)  
  60.             Dim ArialBH18 As Integer = Convert.ToInt32(ArialB18.GetHeight(g))  
  61.             ArialB20 = New Font("Arial", 20, FontStyle.Bold)  
  62.             Dim ArialBH20 As Integer = Convert.ToInt32(ArialB20.GetHeight(g))  
  63.   
  64.             ComputeLeftAll(g)  
  65.   
  66.             Dim myBrush As SolidBrush = New SolidBrush(Color.Black)  
  67.   
  68.             CurrentY = CurrentY + ArialRH14  
  69.             'Draw Report Head:  
  70.             g.DrawString(dcWork, ArialB14, myBrush, xWork, CurrentY) 'dcWorkAR  
  71.             CurrentY = CurrentY + ArialBH14  
  72.             g.DrawString(DocHead1, ArialB18, myBrush, xDocHead1, CurrentY) 'DocHeadAR1  
  73.             CurrentY = CurrentY + ArialBH20  
  74.             g.DrawString(DocHead2, ArialB16, myBrush, xDocHead2, CurrentY) 'DocHeadAR2  
  75.             CurrentY = CurrentY + 2 * ArialBH14  
  76.             ' Draw line  
  77.             g.DrawLine(New Pen(Brushes.Black), leftMargin, CurrentY, rightMargin, CurrentY)  
  78.             CurrentY = CurrentY + ArialRH14  
  79.             ' Draw Personal Data:  
  80.             g.DrawString(RepSickName, ArialB14, myBrush, xSickName, CurrentY)  
  81.             g.DrawString(RepSickNum, ArialB14, myBrush, xSickNum, CurrentY)  
  82.             g.DrawString(RepVisitDate, ArialB14, myBrush, xSickVisit, CurrentY)  
  83.             CurrentY = CurrentY + 2 * ArialBH14  
  84.             ' Draw EyeHorus:  
  85.             g.DrawString(EyeHorus, CorsivaFont, myBrush, CurrentX, CurrentY)  
  86.             CurrentY = CurrentY + CorsivaHeight  
  87.             ' Draw Recipe:  
  88.             CurrentY = CurrentY + ArialRH14  
  89.             g.DrawString(Medicine1, ArialR14, myBrush, CurrentX, CurrentY)  
  90.             CurrentY = CurrentY + ArialRH14  
  91.             g.DrawString(Medicine2, ArialR14, myBrush, CurrentX, CurrentY)  
  92.             CurrentY = CurrentY + ArialRH14  
  93.             g.DrawString(Medicine3, ArialR14, myBrush, CurrentX, CurrentY)  
  94.             ' Go to end of page:  
  95.             CurrentY = Globals.bottomMargin - 4 * ArialRH14  
  96.             ' Draw line  
  97.             g.DrawLine(New Pen(Brushes.Black), leftMargin, CurrentY, rightMargin, CurrentY)  
  98.             ' Draw Report Tail:  
  99.             CurrentY = CurrentY + ArialRH14  
  100.             g.DrawString(RepTail1, ArialB14, myBrush, xRepTail1, CurrentY) 'RepTail1  
  101.   
  102.             ArialB14.Dispose()  
  103.             ArialB16.Dispose()  
  104.             ArialB18.Dispose()  
  105.             ArialB20.Dispose()  
  106.             ArialR14.Dispose()  
  107.             CorsivaFont.Dispose()  
  108.             g.Dispose()  
  109.         Catch  
  110.             Dim Msg As String = "Can't print, no Font!"  
  111.             MessageBox.Show(Msg, "Printing.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)  
  112.         End Try  
  113.   
  114.     End Sub 

Please read the code of Form1 then run the program to see the result.

If you have any idea or if you find any problems then please tell me.