M
MeriMa'at
Guest
In my code I have managed to erase the buttons from my print preview so that all that should appear is the text in my text and list boxes. However, while the text from my text boxes do appear, my listboxes (I have three) look blank in the print preview window.
Here is my code:
PrintPage event raised for each page to be printed
Private Sub Document_PrintPage(Sender As System.Object, e As PrintPageEventArgs)
Dim FontObject As Font variable to store font
store a controls x- and y-coordinates
Dim YPosition As Single
Dim XPosition As Single
represent left margin of page
Dim LeftMargin As Single = e.MarginBounds.Left
represent top margin of page
Dim TopMargin As Single = e.MarginBounds.Top
store a controls text
Dim ControlText As String = Nothing
iterate over the controls on the form, printing the text displayed in each control
For Each ControlObject As Control In Me.Controls
do not print Buttons
If Not (TypeOf ControlObject Is Button) Then
ControlText = ControlObject.Text
FontObject = New Font("Segoe UI", 9.0F, FontStyle.Regular)
set string positions relative to page margins
XPosition = LeftMargin + ControlObject.Location.X
YPosition = TopMargin + ControlObject.Location.Y
draw text in graphics object
e.Graphics.DrawString(ControlText, FontObject, Brushes.Black, XPosition, YPosition)
End If
Next ControlObject
draw line under the recipes name
e.Graphics.DrawLine(Pens.Black,
RecipeNameTextBox.Location.X + LeftMargin,
RecipeNameTextBox.Location.Y + TopMargin + 15,
RecipeNameTextBox.Location.X + RecipeNameTextBox.Width + LeftMargin,
RecipeNameTextBox.Location.Y + TopMargin + 15)
draw box around recipe card
e.Graphics.DrawRectangle(Pens.Black, LeftMargin, TopMargin, Me.Width, Me.Height - 60)
End Sub
Private Sub PrintButton_Click_1(sender As System.Object, e As System.EventArgs) Handles PrintButton.Click
clear out intro label
IntroLabel.Text = ""
create new object to assist in printing
Dim Document As New PrintDocument()
tell PrintDocument where to find PrintPage event handler
AddHandler Document.PrintPage,
AddressOf Document_PrintPage
if no printers are installed, display error message
If PrinterSettings.InstalledPrinters.Count = 0 Then
ErrorMessage()
Return exit event handler
End If
print the document
Document.Print()
End Sub
Private Sub PreviewButton_Click_1(sender As System.Object, e As System.EventArgs) Handles PreviewButton.Click
clear out intro label
IntroLabel.Text = ""
create new object to assist in previewing
Dim Document As New PrintDocument()
tell PrintDocument where to find PrintPage event handler
AddHandler Document.PrintPage,
AddressOf Document_PrintPage
if no printers are installed, display error message
If PrinterSettings.InstalledPrinters.Count = 0 Then
ErrorMessage()
Return exit event handler
End If
PreviewObject.Document = Document specify document
PreviewObject.ShowDialog() show print preview
End Sub
Sub ErrorMessage()
MessageBox.Show("No printers are installed. Please make install a printer to preview or print the document.", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
And this is what my print preview shows, after text has been inputted to all the text and list boxes: (The list boxes are at the bottom under the "Ingredients" label)
Continue reading...
Here is my code:
PrintPage event raised for each page to be printed
Private Sub Document_PrintPage(Sender As System.Object, e As PrintPageEventArgs)
Dim FontObject As Font variable to store font
store a controls x- and y-coordinates
Dim YPosition As Single
Dim XPosition As Single
represent left margin of page
Dim LeftMargin As Single = e.MarginBounds.Left
represent top margin of page
Dim TopMargin As Single = e.MarginBounds.Top
store a controls text
Dim ControlText As String = Nothing
iterate over the controls on the form, printing the text displayed in each control
For Each ControlObject As Control In Me.Controls
do not print Buttons
If Not (TypeOf ControlObject Is Button) Then
ControlText = ControlObject.Text
FontObject = New Font("Segoe UI", 9.0F, FontStyle.Regular)
set string positions relative to page margins
XPosition = LeftMargin + ControlObject.Location.X
YPosition = TopMargin + ControlObject.Location.Y
draw text in graphics object
e.Graphics.DrawString(ControlText, FontObject, Brushes.Black, XPosition, YPosition)
End If
Next ControlObject
draw line under the recipes name
e.Graphics.DrawLine(Pens.Black,
RecipeNameTextBox.Location.X + LeftMargin,
RecipeNameTextBox.Location.Y + TopMargin + 15,
RecipeNameTextBox.Location.X + RecipeNameTextBox.Width + LeftMargin,
RecipeNameTextBox.Location.Y + TopMargin + 15)
draw box around recipe card
e.Graphics.DrawRectangle(Pens.Black, LeftMargin, TopMargin, Me.Width, Me.Height - 60)
End Sub
Private Sub PrintButton_Click_1(sender As System.Object, e As System.EventArgs) Handles PrintButton.Click
clear out intro label
IntroLabel.Text = ""
create new object to assist in printing
Dim Document As New PrintDocument()
tell PrintDocument where to find PrintPage event handler
AddHandler Document.PrintPage,
AddressOf Document_PrintPage
if no printers are installed, display error message
If PrinterSettings.InstalledPrinters.Count = 0 Then
ErrorMessage()
Return exit event handler
End If
print the document
Document.Print()
End Sub
Private Sub PreviewButton_Click_1(sender As System.Object, e As System.EventArgs) Handles PreviewButton.Click
clear out intro label
IntroLabel.Text = ""
create new object to assist in previewing
Dim Document As New PrintDocument()
tell PrintDocument where to find PrintPage event handler
AddHandler Document.PrintPage,
AddressOf Document_PrintPage
if no printers are installed, display error message
If PrinterSettings.InstalledPrinters.Count = 0 Then
ErrorMessage()
Return exit event handler
End If
PreviewObject.Document = Document specify document
PreviewObject.ShowDialog() show print preview
End Sub
Sub ErrorMessage()
MessageBox.Show("No printers are installed. Please make install a printer to preview or print the document.", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
And this is what my print preview shows, after text has been inputted to all the text and list boxes: (The list boxes are at the bottom under the "Ingredients" label)

Continue reading...