برنامج إدارة المبيعات: الدرس التاسع : صفحة البيع النقدي - الجزء 6: طباعة وصل المبيعات
في الدرس التاسع من برنامج ادارة المبيعات سنقوم بطباعة معلومات وصل البيع النقدي إضافةً الى جدول المواد المضافة للوصل.

أولا: إدراج الأداة PrintPreviewDialog.
Imports System.Windows
Public Class EnhancedPrintPreviewDialog1
Inherits
System.Windows.Forms.PrintPreviewDialog
Dim ShowCounter As Boolean = True
Private Sub
myPrintPreview_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
If ShowCounter = True Then
'Get the
toolstrip from the base control
Dim ts As ToolStrip = CType(Me.Controls(1),
ToolStrip)
'Get the print
button from the toolstrip
Dim printItem As ToolStripItem
= ts.Items("printToolStripButton")
'Add a new
button (our own) to the toolstrip by copying properties from the orginal
With printItem
Dim myPrintItem As ToolStripItem
myPrintItem
= ts.Items.Add(.Text, .Image, New EventHandler(AddressOf MyPrintItemClicked))
'Alter as many
other personalized stuff here
myPrintItem.DisplayStyle = ToolStripItemDisplayStyle.Image
'Relocate the item to the beginning of
the toolstrip
ts.Items.Insert(0, myPrintItem)
End With
'Remove the
orginal button
ts.Items.Remove(printItem)
ShowCounter = False
End If
End Sub
Private Sub
MyPrintItemClicked(ByVal sender As Object, ByVal e As EventArgs)
'Allow the user
to choose a printer and specify other settings.
Dim dlgPrint As New PrintDialog
With dlgPrint
.Document = Me.Document
'.UseEXDialog =
True
.AllowSelection
= False
.ShowNetwork = False
.AllowCurrentPage = True
.AllowSomePages
= True
End With
'If the user
clicked OK, print the document.
If
dlgPrint.ShowDialog = Forms.DialogResult.OK Then
Me.Document.Print()
End If
End Sub
Private Sub
previewControl_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub
InitializeComponent()
Me.SuspendLayout()
'
'EnhancedPrintPreviewDialog
'
Me.ClientSize = New
System.Drawing.Size(400, 300)
Me.Name = "EnhancedPrintPreviewDialog"
Me.ResumeLayout(False)
End Sub
End Class
Imports System.Windows
Public Class EnhancedPrintPreviewDialog1
Inherits
System.Windows.Forms.PrintPreviewDialog
Dim ShowCounter As Boolean = True
Private Sub
myPrintPreview_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
If ShowCounter = True Then
'Get the
toolstrip from the base control
Dim ts As ToolStrip = CType(Me.Controls(1),
ToolStrip)
'Get the print
button from the toolstrip
Dim printItem As ToolStripItem
= ts.Items("printToolStripButton")
'Add a new
button (our own) to the toolstrip by copying properties from the orginal
With printItem
Dim myPrintItem As ToolStripItem
myPrintItem
= ts.Items.Add(.Text, .Image, New EventHandler(AddressOf MyPrintItemClicked))
'Alter as many
other personalized stuff here
myPrintItem.DisplayStyle = ToolStripItemDisplayStyle.Image
'Relocate the item to the beginning of
the toolstrip
ts.Items.Insert(0, myPrintItem)
End With
'Remove the
orginal button
ts.Items.Remove(printItem)
ShowCounter = False
End If
End Sub
Private Sub
MyPrintItemClicked(ByVal sender As Object, ByVal e As EventArgs)
'Allow the user
to choose a printer and specify other settings.
Dim dlgPrint As New PrintDialog
With dlgPrint
.Document = Me.Document
'.UseEXDialog =
True
.AllowSelection
= False
.ShowNetwork = False
.AllowCurrentPage = True
.AllowSomePages
= True
End With
'If the user
clicked OK, print the document.
If
dlgPrint.ShowDialog = Forms.DialogResult.OK Then
Me.Document.Print()
End If
End Sub
Private Sub
previewControl_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub
InitializeComponent()
Me.SuspendLayout()
'
'EnhancedPrintPreviewDialog
'
Me.ClientSize = New
System.Drawing.Size(400, 300)
Me.Name = "EnhancedPrintPreviewDialog"
Me.ResumeLayout(False)
End Sub
End Class

ثانياً: إدراج مديول جديد Module.
Imports System.Drawing.Printing
Module DataGridViewPrinter1
Public
DataGridViewToPrint As New DataGridView
Public
DefaultPageSettings As PageSettings = New PageSettings()
Public WithEvents DocToPrint As New PrintDocument
Private lPageNo As String = ""
Private sPageNo As String = ""
Private oStringFormat As StringFormat
Private
oStringFormatComboBox As StringFormat
Private oButton As Button
Private oCheckbox As CheckBox
Private oComboBox As ComboBox
Private nTotalWidth As Int16
Private nRowPos As Int16
Private NewPage As Boolean
Private nPageNo As Int16
Private Header As String
Private FooterComment As String = ""
Public PrintType As String
Public Sub StartPrint(ByVal GridToPrint As DataGridView, ByVal PrintAsLandscape As Boolean, ByVal
ShowPrintPreview As Boolean, ByVal HeaderToPrint As String, ByVal CommentToPrint As String)
DataGridViewToPrint
= GridToPrint
Header =
HeaderToPrint
FooterComment =
CommentToPrint
'DataGridViewToPrint.Columns(6).Visible
= False ' Use to hide a col. (index no.)
' Set up Default
Page Settings
DocToPrint.DefaultPageSettings.Landscape = PrintAsLandscape
DocToPrint.DefaultPageSettings.Margins.Left = 25
DocToPrint.DefaultPageSettings.Margins.Right = 75
DocToPrint.DefaultPageSettings.Margins.Top = 25
DocToPrint.DefaultPageSettings.Margins.Bottom = 75
DocToPrint.OriginAtMargins = True ' takes margins into account
If
ShowPrintPreview = True Then
Dim
dlgPrintPreview As New EnhancedPrintPreviewDialog1
dlgPrintPreview.ClientSize = New System.Drawing.Size(600, 600)
dlgPrintPreview.Document = DocToPrint ' Previews print
dlgPrintPreview.ShowDialog()
Else
' Allow the user to choose a printer and
specify other settings.
Dim dlgPrint As New PrintDialog
With dlgPrint
.AllowSelection = False
.ShowNetwork
= False
.AllowCurrentPage = True
.AllowSomePages = True
.Document =
DocToPrint
End With
' If the user clicked OK, print the document.
If
dlgPrint.ShowDialog = DialogResult.OK Then
DocToPrint.Print()
End If
End If
End Sub
Public Sub
DocToPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles
DocToPrint.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment =
StringAlignment.Center
oStringFormat.LineAlignment = StringAlignment.Center
oStringFormat.Trimming = StringTrimming.EllipsisCharacter
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.LineAlignment
= StringAlignment.Center
oStringFormatComboBox.FormatFlags =
StringFormatFlags.DirectionRightToLeft
oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then ' Prints only Visible columns
nTotalWidth
+= oColumn.Width
End If
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Public Sub DocToPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Handles
DocToPrint.PrintPage
Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static nHeight As Int16
Dim nWidth, i,
nRowsPerPage As Int16
Dim nTop As Int16 =
e.MarginBounds.Top
Dim nLeft As Int16 =
e.MarginBounds.Left
Dim nRight As Int16 =
e.MarginBounds.Right
' Draw Header
Dim Rowscount As Integer =
DataGridViewToPrint.Rows.Count ' get rows
count for table height
' Dim rowCount As Integer
Dim rectC2 As New Rectangle
If frm = "ForwardSales" Then
rectC2 = New Rectangle(45,
30, 730, +350 + (Rowscount * 60)) 'col1 -
border [ Start from right, Start From
top,( Width - end to right),Height - end to down)]
Else
rectC2 = New Rectangle(45,
30, 730, +350 + (Rowscount * 35))
End If
Dim blackPen As New
Pen(Color.Navy, 2)
Dim fntWSL As New Font("Khalid
Art bold", 15, FontStyle.Underline)
Dim fnt As New Font("Arial", 13,
FontStyle.Bold)
Dim fntSe As New Font("Hacen
Freehand", 14, FontStyle.Regular)
Dim fntSe2 As New Font("Hacen
Freehand", 12, FontStyle.Regular)
Dim fnt2 As New Font("Hacen
Promoter Md", 24, FontStyle.Regular)
Dim fntRES As New Font("Fanan", 14,
FontStyle.Regular)
Dim fnt4 As New Font("Tahoma", 13,
FontStyle.Regular)
Dim stringss As New StringFormat
stringss.Alignment =
StringAlignment.Center
Dim stringsL As New StringFormat
stringsL.Alignment =
StringAlignment.Far
Dim stringsN As New StringFormat
stringsN.Alignment =
StringAlignment.Near
If nPageNo = 1 Then
oColumnLefts.Clear()
oColumnWidths.Clear()
oColumnTypes.Clear()
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then
nWidth =
CType(Math.Floor(oColumn.Width
/ nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth) - 10),
Int16) ' -10 decrease table width
nHeight =
e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font,
nWidth).Height + 15 'row height
oColumnLefts.Add(8 + nLeft) 'set table in the center of rectangle
oColumnWidths.Add(nWidth)
oColumnTypes.Add(oColumn.GetType)
nLeft +=
nWidth
End If
Next
End If
Do While nRowPos < DataGridViewToPrint.Rows.Count
Dim oRow As
DataGridViewRow = DataGridViewToPrint.Rows(nRowPos)
If nTop + nHeight
>= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
Dim point1 As New Point(45, 140)
Dim point2 As New Point(775,
140) '
e.Graphics.DrawRectangle(Pens.Navy, rectC2) ' border
column
e.Graphics.DrawLine(blackPen, point1, point2)
'header
e.Graphics.DrawString("اسم الشركة او المكتب", (fnt2),
Brushes.Red, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 30, stringss)
e.Graphics.DrawString("نشاط الشركة - وصف الشركة ", (fntSe), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 70, stringss)
e.Graphics.DrawString(" العنوان : المحافظة - المدينة - نقطة دالة " + " -
" + "هاتف : 07700000000 - 07711111111", (fntSe2),
Brushes.Blue, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 100, stringss)
'contents
If frm = "Bill" Then
e.Graphics.DrawString("وصل بيع نقدي", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmBills.tbDate.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmBills.tbBillID.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmBills.tbCName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "CashSales" Then
e.Graphics.DrawString("وصل بيع نقدي", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top - e.Graphics.MeasureString(Header,
New
Font(DataGridViewToPrint.Font, FontStyle.Bold), e.MarginBounds.Width).Height +
160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmCashSale.BillDate, (fnt),
Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmCashSale.tbBillNo.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmCashSale.tbPurchaserName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "ForwardSales" Then
e.Graphics.DrawString("وصل بيع آجل", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmForwardSales.BillDate,
(fnt), Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmForwardSales.tbBillNo.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmForwardSales.tbPurchaserName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "Forward
Bills" Then
e.Graphics.DrawString("وصل بيع آجل", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmForwardBills.tbDate.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmForwardBills.tbBillID.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmForwardBills.tbCName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top - e.Graphics.MeasureString(Header,
New
Font(DataGridViewToPrint.Font, FontStyle.Bold), e.MarginBounds.Width).Height +
210, stringss)
End If
' Draw Columns
nTop =
e.MarginBounds.Top
i = 0
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i)
+ 23, nTop + 220, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New
SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) +
23, nTop + 220, oColumnWidths(i), nHeight), oStringFormat)
i += 1
End If
Next
NewPage = False
End If
nTop +=
nHeight
i = 0
'draw cells
For Each oCell As
DataGridViewCell In oRow.Cells
If oCell.Visible
= True Then
If
oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn)
Then
If
oCell.Value.ToString = Nothing Then
e.Graphics.DrawString(" ", oCell.InheritedStyle.Font, New
SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight), oStringFormat)
Else
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New
SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight), oStringFormat)
End If
ElseIf
oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New
Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width,
oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf
oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(oCell.Value, Boolean)
Dim oBitmap As New Bitmap(oColumnWidths(i),
nHeight)
Dim oTempGraphics As Graphics =
Graphics.FromImage(oBitmap)
oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0,
0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width
- oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) /
2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf
oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width,
oComboBox.Height)
oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width,
oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCell.Value.ToString,
oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New
RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight),
oStringFormatComboBox)
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn)
Then
Dim oCellSize As Rectangle = New
Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
Dim oImageSize As Size = CType(oCell.Value,
Image).Size
e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width
- oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height -
oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value,
Image).Height))
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight))
i +=
1
End If
Next
'bottom
'e.Graphics.DrawLine(blackPen,
point1, point2)
If frm = "ForwardSales" Then
e.Graphics.DrawString("المبلغ المسدد بالدينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 90, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString(" " + frmForwardSales.tbAmountPaid.Text,
(fnt), Brushes.Green, e.MarginBounds.Right - 215, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString("دينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 295, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 303 + Rowscount * 45, stringss)
e.Graphics.DrawString("المبلغ المتبقي بالدينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 420, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString(" " +
frmForwardSales.lbRemainingAmount.Text, (fnt), Brushes.Red,
e.MarginBounds.Right - 550, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString("دينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 630, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 303 + Rowscount * 45, stringss)
If
frmForwardSales.cbNotifications.Checked = True Then
e.Graphics.DrawString("على ان يتم السداد في تأريخ : " +
frmForwardSales.BillPaymentDate, (fnt), Brushes.Red, e.MarginBounds.Right -
340, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 342 + Rowscount * 45, stringss)
End If
e.Graphics.DrawString(" المشتري", (fntRES),
Brushes.Black, e.MarginBounds.Right - 110, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
e.Graphics.DrawString(" الموظف", (fntRES),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
e.Graphics.DrawString("الختم", (fntRES),
Brushes.Black, e.MarginBounds.Right - 600, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
Else
e.Graphics.DrawString(" المشتري", (fntRES),
Brushes.Black, e.MarginBounds.Right - 110, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
e.Graphics.DrawString(" الموظف", (fntRES),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
e.Graphics.DrawString("الختم", (fntRES),
Brushes.Black, e.MarginBounds.Right - 600, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
End If
End If
nRowPos += 1
nRowsPerPage +=
1
Loop
DrawFooter(e,
nRowsPerPage)
e.HasMorePages = False
End Sub
Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
Dim sPageNo As String =
nPageNo.ToString + " of "
If nPageNo = "1" Then
lPageNo =
Math.Ceiling((DataGridViewToPrint.Rows.Count - 1) / RowsPerPage).ToString()
sPageNo =
nPageNo.ToString + " of " + lPageNo
Else
sPageNo =
nPageNo.ToString + " of " + lPageNo
End If
End Sub
End Module
Imports System.Drawing.Printing
Module DataGridViewPrinter1
Public
DataGridViewToPrint As New DataGridView
Public
DefaultPageSettings As PageSettings = New PageSettings()
Public WithEvents DocToPrint As New PrintDocument
Private lPageNo As String = ""
Private sPageNo As String = ""
Private oStringFormat As StringFormat
Private
oStringFormatComboBox As StringFormat
Private oButton As Button
Private oCheckbox As CheckBox
Private oComboBox As ComboBox
Private nTotalWidth As Int16
Private nRowPos As Int16
Private NewPage As Boolean
Private nPageNo As Int16
Private Header As String
Private FooterComment As String = ""
Public PrintType As String
Public Sub StartPrint(ByVal GridToPrint As DataGridView, ByVal PrintAsLandscape As Boolean, ByVal
ShowPrintPreview As Boolean, ByVal HeaderToPrint As String, ByVal CommentToPrint As String)
DataGridViewToPrint
= GridToPrint
Header =
HeaderToPrint
FooterComment =
CommentToPrint
'DataGridViewToPrint.Columns(6).Visible
= False ' Use to hide a col. (index no.)
' Set up Default
Page Settings
DocToPrint.DefaultPageSettings.Landscape = PrintAsLandscape
DocToPrint.DefaultPageSettings.Margins.Left = 25
DocToPrint.DefaultPageSettings.Margins.Right = 75
DocToPrint.DefaultPageSettings.Margins.Top = 25
DocToPrint.DefaultPageSettings.Margins.Bottom = 75
DocToPrint.OriginAtMargins = True ' takes margins into account
If
ShowPrintPreview = True Then
Dim
dlgPrintPreview As New EnhancedPrintPreviewDialog1
dlgPrintPreview.ClientSize = New System.Drawing.Size(600, 600)
dlgPrintPreview.Document = DocToPrint ' Previews print
dlgPrintPreview.ShowDialog()
Else
' Allow the user to choose a printer and
specify other settings.
Dim dlgPrint As New PrintDialog
With dlgPrint
.AllowSelection = False
.ShowNetwork
= False
.AllowCurrentPage = True
.AllowSomePages = True
.Document =
DocToPrint
End With
' If the user clicked OK, print the document.
If
dlgPrint.ShowDialog = DialogResult.OK Then
DocToPrint.Print()
End If
End If
End Sub
Public Sub
DocToPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles
DocToPrint.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment =
StringAlignment.Center
oStringFormat.LineAlignment = StringAlignment.Center
oStringFormat.Trimming = StringTrimming.EllipsisCharacter
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.LineAlignment
= StringAlignment.Center
oStringFormatComboBox.FormatFlags =
StringFormatFlags.DirectionRightToLeft
oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then ' Prints only Visible columns
nTotalWidth
+= oColumn.Width
End If
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Public Sub DocToPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Handles
DocToPrint.PrintPage
Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static nHeight As Int16
Dim nWidth, i,
nRowsPerPage As Int16
Dim nTop As Int16 =
e.MarginBounds.Top
Dim nLeft As Int16 =
e.MarginBounds.Left
Dim nRight As Int16 =
e.MarginBounds.Right
' Draw Header
Dim Rowscount As Integer =
DataGridViewToPrint.Rows.Count ' get rows
count for table height
' Dim rowCount As Integer
Dim rectC2 As New Rectangle
If frm = "ForwardSales" Then
rectC2 = New Rectangle(45,
30, 730, +350 + (Rowscount * 60)) 'col1 -
border [ Start from right, Start From
top,( Width - end to right),Height - end to down)]
Else
rectC2 = New Rectangle(45,
30, 730, +350 + (Rowscount * 35))
End If
Dim blackPen As New
Pen(Color.Navy, 2)
Dim fntWSL As New Font("Khalid
Art bold", 15, FontStyle.Underline)
Dim fnt As New Font("Arial", 13,
FontStyle.Bold)
Dim fntSe As New Font("Hacen
Freehand", 14, FontStyle.Regular)
Dim fntSe2 As New Font("Hacen
Freehand", 12, FontStyle.Regular)
Dim fnt2 As New Font("Hacen
Promoter Md", 24, FontStyle.Regular)
Dim fntRES As New Font("Fanan", 14,
FontStyle.Regular)
Dim fnt4 As New Font("Tahoma", 13,
FontStyle.Regular)
Dim stringss As New StringFormat
stringss.Alignment =
StringAlignment.Center
Dim stringsL As New StringFormat
stringsL.Alignment =
StringAlignment.Far
Dim stringsN As New StringFormat
stringsN.Alignment =
StringAlignment.Near
If nPageNo = 1 Then
oColumnLefts.Clear()
oColumnWidths.Clear()
oColumnTypes.Clear()
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then
nWidth =
CType(Math.Floor(oColumn.Width
/ nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth) - 10),
Int16) ' -10 decrease table width
nHeight =
e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font,
nWidth).Height + 15 'row height
oColumnLefts.Add(8 + nLeft) 'set table in the center of rectangle
oColumnWidths.Add(nWidth)
oColumnTypes.Add(oColumn.GetType)
nLeft +=
nWidth
End If
Next
End If
Do While nRowPos < DataGridViewToPrint.Rows.Count
Dim oRow As
DataGridViewRow = DataGridViewToPrint.Rows(nRowPos)
If nTop + nHeight
>= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
Dim point1 As New Point(45, 140)
Dim point2 As New Point(775,
140) '
e.Graphics.DrawRectangle(Pens.Navy, rectC2) ' border
column
e.Graphics.DrawLine(blackPen, point1, point2)
'header
e.Graphics.DrawString("اسم الشركة او المكتب", (fnt2),
Brushes.Red, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 30, stringss)
e.Graphics.DrawString("نشاط الشركة - وصف الشركة ", (fntSe), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 70, stringss)
e.Graphics.DrawString(" العنوان : المحافظة - المدينة - نقطة دالة " + " -
" + "هاتف : 07700000000 - 07711111111", (fntSe2),
Brushes.Blue, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 100, stringss)
'contents
If frm = "Bill" Then
e.Graphics.DrawString("وصل بيع نقدي", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmBills.tbDate.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmBills.tbBillID.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmBills.tbCName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "CashSales" Then
e.Graphics.DrawString("وصل بيع نقدي", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top - e.Graphics.MeasureString(Header,
New
Font(DataGridViewToPrint.Font, FontStyle.Bold), e.MarginBounds.Width).Height +
160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmCashSale.BillDate, (fnt),
Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmCashSale.tbBillNo.Text, (fnt),
Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmCashSale.tbPurchaserName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "ForwardSales" Then
e.Graphics.DrawString("وصل بيع آجل", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmForwardSales.BillDate,
(fnt), Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmForwardSales.tbBillNo.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmForwardSales.tbPurchaserName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 210, stringss)
End If
If frm = "Forward
Bills" Then
e.Graphics.DrawString("وصل بيع آجل", (fntWSL),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 160, stringss)
e.Graphics.DrawString("التأريخ
:- " + frmForwardBills.tbDate.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 85, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringss)
e.Graphics.DrawString("رقم الوصل :- " + frmForwardBills.tbBillID.Text,
(fnt), Brushes.Black, e.MarginBounds.Right - 690, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 155, stringsN)
e.Graphics.DrawString("حضرة السيد / السيدة :-
" + frmForwardBills.tbCName.Text, (fntRES), Brushes.Black,
e.MarginBounds.Right - 345, e.MarginBounds.Top - e.Graphics.MeasureString(Header,
New
Font(DataGridViewToPrint.Font, FontStyle.Bold), e.MarginBounds.Width).Height +
210, stringss)
End If
' Draw Columns
nTop =
e.MarginBounds.Top
i = 0
For Each oColumn As
DataGridViewColumn In DataGridViewToPrint.Columns
If
oColumn.Visible = True Then
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i)
+ 23, nTop + 220, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New
SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) +
23, nTop + 220, oColumnWidths(i), nHeight), oStringFormat)
i += 1
End If
Next
NewPage = False
End If
nTop +=
nHeight
i = 0
'draw cells
For Each oCell As
DataGridViewCell In oRow.Cells
If oCell.Visible
= True Then
If
oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn)
Then
If
oCell.Value.ToString = Nothing Then
e.Graphics.DrawString(" ", oCell.InheritedStyle.Font, New
SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight), oStringFormat)
Else
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New
SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight), oStringFormat)
End If
ElseIf
oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New
Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width,
oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf
oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(oCell.Value, Boolean)
Dim oBitmap As New Bitmap(oColumnWidths(i),
nHeight)
Dim oTempGraphics As Graphics =
Graphics.FromImage(oBitmap)
oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0,
0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width
- oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) /
2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf
oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width,
oComboBox.Height)
oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width,
oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCell.Value.ToString,
oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New
RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight),
oStringFormatComboBox)
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn)
Then
Dim oCellSize As Rectangle = New
Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
Dim oImageSize As Size = CType(oCell.Value,
Image).Size
e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width
- oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height -
oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value,
Image).Height))
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i) + 23, nTop
+ 220, oColumnWidths(i), nHeight))
i +=
1
End If
Next
'bottom
'e.Graphics.DrawLine(blackPen,
point1, point2)
If frm = "ForwardSales" Then
e.Graphics.DrawString("المبلغ المسدد بالدينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 90, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString(" " + frmForwardSales.tbAmountPaid.Text,
(fnt), Brushes.Green, e.MarginBounds.Right - 215, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString("دينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 295, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 303 + Rowscount * 45, stringss)
e.Graphics.DrawString("المبلغ المتبقي بالدينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 420, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString(" " +
frmForwardSales.lbRemainingAmount.Text, (fnt), Brushes.Red,
e.MarginBounds.Right - 550, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 302 + Rowscount * 45, stringss)
e.Graphics.DrawString("دينار ", (fnt), Brushes.Black,
e.MarginBounds.Right - 630, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 303 + Rowscount * 45, stringss)
If
frmForwardSales.cbNotifications.Checked = True Then
e.Graphics.DrawString("على ان يتم السداد في تأريخ : " +
frmForwardSales.BillPaymentDate, (fnt), Brushes.Red, e.MarginBounds.Right -
340, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 342 + Rowscount * 45, stringss)
End If
e.Graphics.DrawString(" المشتري", (fntRES),
Brushes.Black, e.MarginBounds.Right - 110, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
e.Graphics.DrawString(" الموظف", (fntRES),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
e.Graphics.DrawString("الختم", (fntRES),
Brushes.Black, e.MarginBounds.Right - 600, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 60, stringss)
Else
e.Graphics.DrawString(" المشتري", (fntRES),
Brushes.Black, e.MarginBounds.Right - 110, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
e.Graphics.DrawString(" الموظف", (fntRES),
Brushes.Black, e.MarginBounds.Right - 345, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
e.Graphics.DrawString("الختم", (fntRES),
Brushes.Black, e.MarginBounds.Right - 600, e.MarginBounds.Top -
e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font,
FontStyle.Bold), e.MarginBounds.Width).Height + 345 + Rowscount * 35, stringss)
End If
End If
nRowPos += 1
nRowsPerPage +=
1
Loop
DrawFooter(e,
nRowsPerPage)
e.HasMorePages = False
End Sub
Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
Dim sPageNo As String =
nPageNo.ToString + " of "
If nPageNo = "1" Then
lPageNo =
Math.Ceiling((DataGridViewToPrint.Rows.Count - 1) / RowsPerPage).ToString()
sPageNo =
nPageNo.ToString + " of " + lPageNo
Else
sPageNo =
nPageNo.ToString + " of " + lPageNo
End If
End Sub
End Module

ثالثاً: زر الطباعة btnPrint.
Private Sub
btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim d, m, y As String
d = cbDay.Text
m = cbMonth.Text
y = cbYear.Text
frm = "CashSales"
BillDate = d & "-" & m & "-" & y
DataGridViewPrinter1.StartPrint(dgvBill, False, True, " ", " ")
End
Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim d, m, y As String
d = cbDay.Text
m = cbMonth.Text
y = cbYear.Text
frm = "CashSales"
BillDate = d & "-" & m & "-" & y
DataGridViewPrinter1.StartPrint(dgvBill, False, True, " ", " ")
End Sub
يمكنكم متابعة الدرس عن طريق اليوتيوب




تعليقات
إرسال تعليق