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

I'm working on this project for class I steam read theses products into a list box on my form I made in VB2005. I'm having an issue when I go to print tho. My first problem is that I have a list box a print button and a report button and also and exit button. When the User Orders one of my products I made it so the print button is enabled and prints out the product selected so far tho instead of printing out just what the user selected it prints out all the products. Could you please look at me code and tell me If I'm doing this right I'll post the whole thing but again the print statement is the only thing I need an answer to if somone would please look at it thanks. Again I'm just posting the whole code so you can see my logic the line in Print Selected is the problem I said print selected item and it gave me an error thanks alot again.

e.Graphics.DrawString(CStr(lstProducts.Items(intListIndex.SelectedIndex)), fntPrintFont, Brushes.Black, sngX, sngY)sngY += sngLineHeight

This line right here seeems to be giving me the most trouble I put selectedIndex in so that when I click print it would only show the product selected. But VB keeps giving me an error message saying.

'Selected index' is not a member of integer'

Dim intListIndex As Integer

what i understand from your code that intListIndex is integer , so you can not write
intListIndex.SelectedIndex
to print only what the user select try this

e.Graphics.DrawString(lstProducts.SelectedItem, fntPrintFont, Brushes.Black, sngX, sngY)

Well that Works with the products but I want the price too the price is declared up above and is read with the code.
Dim sngPrice As Single
I tried this to print out the price

e.Graphics.DrawString(CStr(lblCost.Text(lstProducts.SelectedIndex)), fntPrintFont, Brushes.Black, sngX, sngY)

it didnt work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.