I want the print button below to print out whatever the user orders and not the entire order list. The List is steam read into the list box I made. My problem is when I click the print button it displays all the products in the print preview instead of the item ordered. I tried selected item for the print button thinking it would only show the selected item in the print preview with no luck could I get some help please?
Public Class Form1
Structure ProductRecord
Dim strProduct As String
Dim intQuantity As Integer
Dim sngPrice As Single
End Structure
Dim ProRec(10) As ProductRecord
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' End the application
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intIndex As Integer
Dim strFileNameIn As String
Try
btnPrint.Enabled = False
Dim Reader As New IO.StreamReader("products.txt")
strFileNameIn = "products.txt"
intIndex = 0
Do Until Reader.Peek = -1
With Reader
ProRec(intIndex).strProduct = .ReadLine()
ProRec(intIndex).intQuantity = CInt(.ReadLine())
ProRec(intIndex).sngPrice = CSng(.ReadLine())
lstProducts.Items.Add(ProRec(intIndex).strProduct)
End With
intIndex += 1
Loop
lstProducts.SelectedIndex = 0
Reader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error in Reading file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub lstProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProducts.SelectedIndexChanged
lblPrice.Text = ("$") & CStr((ProRec(lstProducts.SelectedIndex).sngPrice))
End Sub
Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click
Dim outputfile As StreamWriter
'If lstProducts.SelectedIndex = -1 Then
'MsgBox("Select a prouduct", MsgBoxStyle.Critical)
' Else
' Select Case CStr(lstProducts.SelectedIndex)
' Case CStr(0) = nudQuantity.Value = 5
' MsgBox("Select a prouduct", MsgBoxStyle.Critical)
' Case CStr(1)
' MsgBox("Select a prouduct", MsgBoxStyle.Critical)
' Case CStr(2)
' MsgBox("Select a prouduct", MsgBoxStyle.Critical)
' End Select
'End If
outputfile = AppendText("C:outputfile.txt")
lblItems.Text = ProRec(lstProducts.SelectedIndex).strProduct
lblCost.Text = ("$") & CStr(CSng(lblPrice.Text) * nudQuantity.Value)
outputfile.WriteLine(lblItems.Text)
outputfile.WriteLine(lblCost.Text)
outputfile.Close()
btnOrder.Enabled = False
btnPrint.Enabled = True
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
PrintPreviewSelected.Document = PrintSelected
PrintPreviewSelected.PrintPreviewControl.Zoom = 1
PrintPreviewSelected.ShowDialog()
End Sub
Private Sub PrintPreviewDialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewProducts.Load
End Sub
Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click
PrintPreviewProducts.Document = PrintReport
PrintPreviewProducts.PrintPreviewControl.Zoom = 1
PrintPreviewProducts.ShowDialog()
End Sub
Private Sub PrintReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintReport.PrintPage
Dim sngX As Single
Dim sngY As Single
Dim fntPrintFont As New Font("Arial", 12)
Dim sngLineHeight As Single = fntPrintFont.GetHeight + 2
Dim intListIndex As Integer
sngX = e.MarginBounds.Left
sngY = e.MarginBounds.Top
'print heading
e.Graphics.DrawString("Iventory Report", fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
sngY += sngLineHeight
e.Graphics.DrawString("Products Quantity Price", fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
sngY += sngLineHeight
'display list of Products
For intListIndex = 0 To lstProducts.Items.Count - 1
e.Graphics.DrawString(CStr(lstProducts.Items(intListIndex)), fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
Next
sngY += sngLineHeight
sngY += sngLineHeight
End Sub
Private Sub PrintSelected_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintSelected.PrintPage
Dim sngX As Single
Dim sngY As Single
Dim fntPrintFont As New Font("Arial", 12)
Dim sngLineHeight As Single = fntPrintFont.GetHeight + 2
Dim intListIndex As Integer
sngX = e.MarginBounds.Left
sngY = e.MarginBounds.Top
'print heading
e.Graphics.DrawString("Order", fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
sngY += sngLineHeight
e.Graphics.DrawString("Products Cost", fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
sngY += sngLineHeight
'display list of Products
For intListIndex = 0 To lstProducts.Items.Count - 1
e.Graphics.DrawString(CStr(lstProducts.Items(intListIndex)), fntPrintFont, Brushes.Black, sngX, sngY)
sngY += sngLineHeight
Next
sngY += sngLineHeight
sngY += sngLineHeight
End Sub
End Class