Goran Bibic

Goran Bibic

  • 455
  • 2.9k
  • 178k

Print receipt from pos printer and add datagrid values c#

Feb 11 2019 9:45 AM
Datagrid rows number, name, tax and price

Need to add values on receipt from datagrid


Some help?
 
  1. <pre>using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Drawing.Printing;    
  7. using System.Linq;    
  8. using System.Text;    
  9. using System.Threading.Tasks;    
  10. using System.Windows.Forms;    
  11.     
  12. namespace POS_TEST    
  13. {    
  14.     public partial class Form1 : Form    
  15.     {    
  16.         public static string barKod = "";    
  17.         public static string osobaIme = "";    
  18.         public static string datumUlaska = "";    
  19.         public static string osobaFirma = "";    
  20.     
  21.     
  22.         public Form1()    
  23.         {    
  24.             InitializeComponent();    
  25.         }    
  26.     
  27.         private void button1_Click(object sender, EventArgs e)    
  28.         {    
  29.             BarCodePrint.PrintReceiptForTransaction(barKod, osobaComboBox.Text, vrijemePrijavePicker.Value.ToString("dd.MM.yyyy. HH:mm:00"), firmaComboBox.Text);    
  30.     
  31.         }    
  32.     
  33.     
  34.         private static void PrintReceiptPage(object sender, PrintPageEventArgs e)    
  35.         {    
  36.     
  37.         }    
  38.     
  39.         class BarCodePrint    
  40.         {    
  41.             public static string barKod = "";    
  42.             public static string osobaIme = "";    
  43.             public static string datumUlaska = "";    
  44.             public static string osobaFirma = "";    
  45.             public static void PrintReceiptForTransaction(string bK, string osoba, string datum, string firma)    
  46.             {    
  47.                 barKod = bK;    
  48.                 osobaIme = osoba;    
  49.                 datumUlaska = datum;    
  50.                 osobaFirma = firma;    
  51.                 PrintDocument recordDoc = new PrintDocument();    
  52.     
  53.                 recordDoc.DocumentName = "BarKod gosta";    
  54.                 recordDoc.PrintPage += new PrintPageEventHandler(BarCodePrint.PrintReceiptPage); // function below    
  55.                 recordDoc.PrintController = new StandardPrintController(); // hides status dialog popup    
  56.                                                                            // Comment if debugging     
  57.                 PrinterSettings ps = new PrinterSettings();    
  58.                 ps.PrinterName = "POS-58";    
  59.                 recordDoc.PrinterSettings = ps;    
  60.                 recordDoc.Print();    
  61.                 // --------------------------------------    
  62.                 // Uncomment if debugging - shows dialog instead    
  63.                 //PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();    
  64.                 //printPrvDlg.Document = recordDoc;    
  65.                 //printPrvDlg.Width = 1200;    
  66.                 //printPrvDlg.Height = 800;    
  67.                 //printPrvDlg.ShowDialog();    
  68.                 // --------------------------------------    
  69.                 recordDoc.Dispose();    
  70.     
  71.             }    
  72.     
  73.     
  74.     
  75.     
  76.     
  77.             private static void PrintReceiptPage(object sender, PrintPageEventArgs e)    
  78.             {    
  79.                 float x = 10;    
  80.                 float y = 5;    
  81.                 float width = 170.0F; // max width I found through trial and error    
  82.                 float height = 0F;    
  83.     
  84.                 Font drawFontArial12Bold = new Font("Arial", 10, FontStyle.Bold);    
  85.                 Font drawFontArial11Regular = new Font("Arial", 8, FontStyle.Regular);    
  86.                 Font drawFontArial7Regular = new Font("Arial", 8, FontStyle.Regular);    
  87.                 Font drawFontArial7Italic = new Font("Arial", 8, FontStyle.Italic);    
  88.                 SolidBrush drawBrush = new SolidBrush(Color.Black);    
  89.     
  90.                 // Set format of string.    
  91.                 StringFormat drawFormatCenter = new StringFormat();    
  92.                 drawFormatCenter.Alignment = StringAlignment.Center;    
  93.                 StringFormat drawFormatLeft = new StringFormat();    
  94.                 drawFormatLeft.Alignment = StringAlignment.Near;    
  95.                 StringFormat drawFormatRight = new StringFormat();    
  96.                 drawFormatRight.Alignment = StringAlignment.Far;    
  97.     
  98.                 // Draw string to screen.    
  99.     
  100.                 string text = "T.R. LAD S.P.";    
  101.                 e.Graphics.DrawString(text, drawFontArial12Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  102.                 y += e.Graphics.MeasureString(text, drawFontArial12Bold).Height;    
  103.     
  104.                 text = "22. BRIGADE VRS, Kotor Varoš";    
  105.                 e.Graphics.DrawString(text, drawFontArial7Italic, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  106.                 y += e.Graphics.MeasureString(text, drawFontArial7Italic).Height;    
  107.     
  108.                 text = "- MALOPRODAJNI RACUN -\n";    
  109.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  110.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;    
  111.     
  112.     
  113.     
  114.                 text = " ";    
  115.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  116.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;    
  117.                 text = " ";    
  118.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  119.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;    
  120.     
  121.     
  122.                 text = " ";    
  123.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  124.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;    
  125.     
  126.     
  127. //////////////////////////////////////////////////////////////////////////////////  
  128.   
  129.   
  130. HERE NEED TO ADD VALUES FROM DATAGRID....SOME HELP?  
  131.   
  132.   
  133. ///////////////////////////////////////////////////////////////////////////////////    
  134.                 // ... and so on    
  135.     
  136.                 //BarcodeDraw bdraw = BarcodeDrawFactory.GetSymbology(BarcodeSymbology.Code128);    
  137.                 //Image barCodeImeage = bdraw.Draw(barKod, 60);    
  138.                 //Console.WriteLine(x + " " + y + " " + width + " " + height);    
  139.                 //e.Graphics.DrawImage(barCodeImeage, new RectangleF(x, y, width, 60));    
  140.                 //y += 65;    
  141.                 //Console.WriteLine(barCodeImeage.Size);    
  142.     
  143.                 text = osobaIme;    
  144.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  145.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;    
  146.     
  147.                 //text = osobaFirma;    
  148.                 //e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  149.                 //y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;    
  150.     
  151.     
  152.                 text = "----------------------------------------";    
  153.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  154.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;    
  155.     
  156.                 text = "Datum: " + DateTime.Now.ToString();    
  157.                 e.Graphics.DrawString(text, drawFontArial7Italic, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  158.                 y += e.Graphics.MeasureString(text, drawFontArial7Italic).Height;    
  159.     
  160.     
  161.     
  162.                 text = "----------------------------------------";    
  163.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  164.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;    
  165.     
  166.     
  167.                 text = "HVALA VAM NA POSJETI";    
  168.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    
  169.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;    
  170.             }    
  171.         }    
  172.     
  173.         private void roba_uslugeBindingNavigatorSaveItem_Click(object sender, EventArgs e)    
  174.         {    
  175.             this.Validate();    
  176.             this.roba_uslugeBindingSource.EndEdit();    
  177.             this.tableAdapterManager.UpdateAll(this.lAD_2019DataSet);    
  178.     
  179.         }    
  180.     
  181.         private void Form1_Load(object sender, EventArgs e)    
  182.         {    
  183.             // TODO: This line of code loads data into the 'lAD_2019DataSet.roba_usluge' table. You can move, or remove it, as needed.    
  184.             this.roba_uslugeTableAdapter.Fill(this.lAD_2019DataSet.roba_usluge);    
  185.     
  186.         }    
  187.     }    
  188. }  </pre> 
 

Answers (1)