ucla11
asked on
Add print button and Printing from a ListBox
I use VB.net every once in awhile to make programs for my kids. So my skill level varies at times. I made a state and capitals game. A long time ago and now my youngest child is learning the States and Capitals. Her teacher wants to use the game in school but I feel it is not that robust. Needs tweaks here and there. So I'll start with this problem. On the main form there is a button that shows your correct answers when you click on this button it open another form that shows correct answers in a listbox, I want have a print button on that form and to print the contents of listbox called lstAnwsers. Actually I would also like also the incorrect ones on the printed page separated by tiles Correct answers and Incorrect answers. I also have a form that loads first when you open the game so that the player can type in their name in a textbox called txtReplace. So perhaps place their name on top page.
This code populates correct answer listbox and removes states and capitals their listboxes once player enters a correct choice, they select state in one listbox and capital in another listbox then click a button to check there answer, these listboxes are side by side on main form. Crude but it works"
If lstStates.SelectedItem = "Alabama" And lstCapitals.SelectedItem = "Montgomery" Then
'adds to list box' CorrectAnwsers.lstAnwsers. Items.Add( lstStates. SelectedIt em & " - " & lstCapitals.SelectedItem)
lstCapitals.Items.RemoveAt (lstCapita ls.Selecte dIndex)
lstStates.Items.RemoveAt(l stStates.S electedInd ex)
lblCorrect.Visible = True
lblWrong.Visible = False
ElseIf lstStates.SelectedItem = "Alaska" And lstCapitals.SelectedItem = "Juneau" Then
CorrectAnwsers.lstAnwsers. Items.Add( lstStates. SelectedIt em & " - " & lstCapitals.SelectedItem)
lstCapitals.Items.RemoveAt (lstCapita ls.Selecte dIndex)
lstStates.Items.RemoveAt(l stStates.S electedInd ex)
lblCorrect.Visible = True
lblWrong.Visible = False
'and so on for each state...........'
The game uses different play versions one is 50 tries, another is on a timer (one minute to five minutes) and unlimited tries. Once the player runs out of time or usestheir tries the states and capital that were not correct remain in their listboxes. so how could I add these to a listbox then print them on the same "results" page. I don't know how to the unlimited try version, perhaps have Quit button and then populate incorrect listbox.
Thanks for your input
This code populates correct answer listbox and removes states and capitals their listboxes once player enters a correct choice, they select state in one listbox and capital in another listbox then click a button to check there answer, these listboxes are side by side on main form. Crude but it works"
If lstStates.SelectedItem = "Alabama" And lstCapitals.SelectedItem = "Montgomery" Then
'adds to list box' CorrectAnwsers.lstAnwsers.
lstCapitals.Items.RemoveAt
lstStates.Items.RemoveAt(l
lblCorrect.Visible = True
lblWrong.Visible = False
ElseIf lstStates.SelectedItem = "Alaska" And lstCapitals.SelectedItem = "Juneau" Then
CorrectAnwsers.lstAnwsers.
lstCapitals.Items.RemoveAt
lstStates.Items.RemoveAt(l
lblCorrect.Visible = True
lblWrong.Visible = False
'and so on for each state...........'
The game uses different play versions one is 50 tries, another is on a timer (one minute to five minutes) and unlimited tries. Once the player runs out of time or usestheir tries the states and capital that were not correct remain in their listboxes. so how could I add these to a listbox then print them on the same "results" page. I don't know how to the unlimited try version, perhaps have Quit button and then populate incorrect listbox.
Thanks for your input
ASKER
I get an error message saying Handles clause requires a WithEvents variable defined in the containing type or one of its base types
Handles PrintDocument1.PrintPage.. .... PrintDocument1 has a sqiggly line under it showing an error
I am using VB.net stuido 2008
Private Sub PrintDocument1_PrintPage(B yVal sender As System.Object, ByVal e As System.Drawing.Printing.Pr intPageEve ntArgs) Handles PrintDocument1.PrintPage
Dim LeftMargin As Single = e.MarginBounds.Left
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim Counter As Integer = 0
Dim CurrentLine As String
Dim m_intItemCounter As Integer
Dim myFont As New Font("Courier New", 16, FontStyle.Regular, GraphicsUnit.Pixel)
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic s)
'Prints each line of file, but stops at end of page
While m_intItemCounter <= lstAnwsers.Items.Count - 1
CurrentLine = CType(lstAnwsers.Items(m_i ntItemCoun ter), String)
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic s)
e.Graphics.DrawString(Curr entLine, myFont, Brushes.Black, LeftMargin, YPosition, New StringFormat())
Counter += 1
m_intItemCounter += 1
End While
'if more line exist, print more pages
If Not (m_intItemCounter = lstAnwsers.Items.Count) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Handles PrintDocument1.PrintPage..
I am using VB.net stuido 2008
Private Sub PrintDocument1_PrintPage(B
Dim LeftMargin As Single = e.MarginBounds.Left
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim Counter As Integer = 0
Dim CurrentLine As String
Dim m_intItemCounter As Integer
Dim myFont As New Font("Courier New", 16, FontStyle.Regular, GraphicsUnit.Pixel)
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic
'Prints each line of file, but stops at end of page
While m_intItemCounter <= lstAnwsers.Items.Count - 1
CurrentLine = CType(lstAnwsers.Items(m_i
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic
e.Graphics.DrawString(Curr
Counter += 1
m_intItemCounter += 1
End While
'if more line exist, print more pages
If Not (m_intItemCounter = lstAnwsers.Items.Count) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and in the top of your class - add this
Imports System.Drawing.Printing
Imports System.Drawing.Printing
ASKER
When I print the intems from list box I want to print plyers name at top of page followed b the date on next line folled by the text "Your Correct Anwsers" then the list of correct anwsers. then and seccond tiltle "you Inccorrect Anwser" followed the remaining contents of lstStates ( the that were not moved to the correct anwser list box) every wors fine with the code you gave I change it to print Players name but when I try to print todays date on next line it prints over the players name so i just added it the same line. Thanks.....
ASKER
Let me correct my spellin errors.........
When I print the intems from list box I want to print players name at top of page followed by the date on next line followed by the text "Your Correct Anwsers" then the list of correct anwsers.from lstCorrectAnwsers then and seccond tiltle "you Inccorrect Anwser" followed the remaining contents of lstStates ( the that were not moved to the correct anwser list box) every works fine with the code you gave me. I changed it to print Players name at top, but when I try to print todays date on next line it prints over the players name so i just added it the same line. Thanks..... here the code
Private Sub btnPrintCorrect_Click(ByVa l sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintCorrect.Click
Try
' Assumes the default printer.
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", _
ex.ToString())
End Try
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim itemcounter As Integer
Dim LeftMargin As Integer = 50
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim YpositionName As Single = 0
Dim Counter As Integer = 0
Dim Counter2 As Integer = 0
Dim CurrentLine As String
Dim PlayersName As String
Dim myFont As New Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Pixel)
Dim myFontPlayer As New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel)
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic s)
'Prints players name and date
YpositionName = TopMargin + Counter * myFontPlayer.GetHeight(e.G raphics)
PlayersName = CType(lblPlayer.Text & "'s" & " correct anwsers " & "on " & Today, String)
e.Graphics.DrawString(Play ersName, myFontPlayer, Brushes.Black, LeftMargin, YpositionName, New StringFormat())
' to drop list of anwsers dow? Counter += 5
'Prints each line of file, but stops at end of page
While itemcounter <= lstCorrectAnwsers.Items.Co unt - 1
CurrentLine = CType(lstCorrectAnwsers.It ems(itemco unter), String)
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic s)
e.Graphics.DrawString(Curr entLine, myFont, Brushes.Black, LeftMargin, YPosition, New StringFormat())
Counter += 1
itemcounter += 1
End While
'if more line exist, print more pages
If Not (itemcounter = lstCorrectAnwsers.Items.Co unt) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
When I print the intems from list box I want to print players name at top of page followed by the date on next line followed by the text "Your Correct Anwsers" then the list of correct anwsers.from lstCorrectAnwsers then and seccond tiltle "you Inccorrect Anwser" followed the remaining contents of lstStates ( the that were not moved to the correct anwser list box) every works fine with the code you gave me. I changed it to print Players name at top, but when I try to print todays date on next line it prints over the players name so i just added it the same line. Thanks..... here the code
Private Sub btnPrintCorrect_Click(ByVa
Try
' Assumes the default printer.
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", _
ex.ToString())
End Try
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim itemcounter As Integer
Dim LeftMargin As Integer = 50
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim YpositionName As Single = 0
Dim Counter As Integer = 0
Dim Counter2 As Integer = 0
Dim CurrentLine As String
Dim PlayersName As String
Dim myFont As New Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Pixel)
Dim myFontPlayer As New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel)
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic
'Prints players name and date
YpositionName = TopMargin + Counter * myFontPlayer.GetHeight(e.G
PlayersName = CType(lblPlayer.Text & "'s" & " correct anwsers " & "on " & Today, String)
e.Graphics.DrawString(Play
' to drop list of anwsers dow? Counter += 5
'Prints each line of file, but stops at end of page
While itemcounter <= lstCorrectAnwsers.Items.Co
CurrentLine = CType(lstCorrectAnwsers.It
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic
e.Graphics.DrawString(Curr
Counter += 1
itemcounter += 1
End While
'if more line exist, print more pages
If Not (itemcounter = lstCorrectAnwsers.Items.Co
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
try play with this
Dim dlg As New PrintPreviewDialog()
Private WithEvents mDoc As New PrintDocument()
Private mFont As New Font("Courier New", 12)
Private Sub PrintDocument1_Click(ByVal
Try
dlg.Document = mDoc
dlg.ShowDialog()
'mDoc.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing", _
ex.ToString())
End Try
End Sub
Private Sub mDoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.Pr
End Sub
Private Sub mDoc_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.Pr
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub mDoc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles mDoc.PrintPage
Dim myFont As New Font("Courier New", 16, FontStyle.Regular, GraphicsUnit.Pixel)
Dim textHeight As Integer = e.Graphics.MeasureString("
Dim value As String = ""
value = " This is a test" & Space(25) & "And scores"
e.Graphics.DrawString(valu
Dim blackPen As New Pen(Color.Black, 1)
' Create points that define line.
Dim point1 As New Point(10, 60)
Dim point2 As New Point(815, 60)
' Draw line to screen.
e.Graphics.DrawLine(blackP
Dim mFont As New Font("Arial", 10)
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim Counter As Integer = 0
Dim CurrentLine As String
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic
'Prints each line of file, but stops at end of page
While itemcounter <= lstDisplay.Items.Count - 1
CurrentLine = CType(lstDisplay.Items(ite
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic
e.Graphics.DrawString(Curr
Counter += 1
itemcounter += 1
End While
'if more line exist, print more pages
If Not (itemcounter = lstDisplay.Items.Count) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub PrintDocument1_PrintPage(B
Dim LeftMargin As Single = e.MarginBounds.Left
Dim TopMargin As Single = e.MarginBounds.Top
Dim MyLines As Single = 0
Dim YPosition As Single = 0
Dim Counter As Integer = 0
Dim CurrentLine As String
Dim myFont As New Font("Courier New", 16, FontStyle.Regular, GraphicsUnit.Pixel)
'calculate number of lines
MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphic
'Prints each line of file, but stops at end of page
While ItemCounter <= lstDisplay.Items.Count - 1
CurrentLine = CType(lstDisplay.Items(Ite
YPosition = TopMargin + Counter * myFont.GetHeight(e.Graphic
e.Graphics.DrawString(Curr
Counter += 1
ItemCounter += 1
End While
'if more line exist, print more pages
If Not (ItemCounter = lstDisplay.Items.Count) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.PrintDocument1.Print()
End Sub
vbturbo