I am writing a vb program to print out fragrances for this auto center. my program is working fine, but i am having trouble writing a for/next loop to print out the interior and exterior lists. the printout has to contain the package name, the interior and exterior items included and the fragrance selected.

can anyone help?

Can you be more specific? Do you know how to use a For...next loop or do you need to know how to retrieve the data from the lists?

The lists have a property that will tell you how many items are in the list. You can do a loop through all the items in the list and put them somewhere.

I dont know how to use a for/next loop at all. The program that i was working on wanted the order printout to contain the package name (standard, deluxe, executive, or luxury), the interior and exterior items, and the fragrance that was selected. We have to use the loop to print the interior and exterior lists. Problem is, we have to show a print preview box showing what they selected broken off into sections. I got it to print, but doesn't look right. I will send what i have in the next reply

Public Class Form1
    Const INTERIOR1_String As String = "Fragrance"
    Const INTERIOR2_String As String = "Shampoo Carpets"
    Const INTERIOR3_String As String = "Shampoo Upholstery"
    Const INTERIOR4_String As String = "Interior Protection Coat"
    Const INTERIOR5_String As String = "Scotchgard"
    Const EXTERIOR1_String As String = "Hand Wash"
    Const EXTERIOR2_String As String = "Hand Wax"
    Const EXTERIOR3_String As String = "Check Engine Fluids"
    Const EXTERIOR4_String As String = "Detail Under Carriage"




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub packageListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles packageListBox.SelectedIndexChanged
        With Me
            'Clear the lists used for display
            .interiorListBox.Items.Clear()
            .exteriorListBox.Items.Clear()


            'Display the options in a Standard Detail Package
            
            If packageListBox.SelectedIndex = 0 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)

                'Display the options in a Deluxe Detail Package              
            ElseIf packageListBox.SelectedIndex = 1 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)

                'Display the options in a Luxury Detail Package
            ElseIf packageListBox.SelectedIndex = 2 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .interiorListBox.Items.Add(INTERIOR3_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR3_String)

                'Display the options in an Executive Detail Package
            ElseIf packageListBox.SelectedIndex = 3 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .interiorListBox.Items.Add(INTERIOR3_String)
                .interiorListBox.Items.Add(INTERIOR4_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR3_String)
                .exteriorListBox.Items.Add(EXTERIOR4_String)

            End If

        End With
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'End the program

        Me.Close()
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        'Clear the fragrance list.
        fragranceComboBox.SelectedIndex = -1


        interiorListBox.Items.Clear()
        exteriorListBox.Items.Clear()

    End Sub

    Private Sub printOrderPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printOrderPrintDocument.PrintPage

        Dim printFont As New Font("Arial", 12)
        Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
        Dim lineHeightSingle As Single = printFont.GetHeight + 2
        Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left
        Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
        Dim printLineString As String
        Dim listIndexInteger As Integer

       


        'Set up and display heading lines.
        printLineString = "Valley Boulevard Auto Center"
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        printLineString = "Programmed by: Sandra Smith"
        verticalPrintLocationSingle += lineHeightSingle
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        'Leave a blank line etween the heading and detail line.

        'Set up the selected line.
        printLineString = "Your Package includes:" & "Exterior: " & Me.exteriorListBox.Text
        verticalPrintLocationSingle += lineHeightSingle * 2
        e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        printLineString = "Interior: " & Me.interiorListBox.Text
        verticalPrintLocationSingle += lineHeightSingle * 2
        e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        printLineString = "Fragrance: " & Me.fragranceComboBox.Text
        'Send the line to the graphics page oject.
        e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        'Loop through the entire list.
        For listIndexInteger = 0 To Me.fragranceComboBox.Items.Count - 1
            'Increment the Y position for the next line.
            verticalPrintLocationSingle += lineHeightSingle

            'Set up a line.
            printLineString = Me.fragranceComboBox.Items(listIndexInteger).ToString()

            'Send the line to the graphics page object.
            e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        Next listIndexInteger
    End Sub

    Private Sub PrintOrderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintOrderToolStripMenuItem.Click


        printOrderPrintDocument.Print()
    End Sub
End Class

Well, the first issue is that, in order to access the text in the listboxes, you need to use the following property instead of Me.exteriorListBox.Text:

Me.exteriorlistbox.Items.Item(index)

For example, to get the test of the first item, use Me.exteriorlistbox.Items.Item(0)

You then need to loop through the listbox so each of the items are printed, which is when you need the For Next loop.

As far as formatting... you just need to make sure that you put new lines correctly and print the printLineString string to the document.

Below is updated code. You will probably need to add/remove spaces throughout the report and change the font... I left the busy work to you :)

Public Class Form1
    Const INTERIOR1_String As String = "Fragrance"
    Const INTERIOR2_String As String = "Shampoo Carpets"
    Const INTERIOR3_String As String = "Shampoo Upholstery"
    Const INTERIOR4_String As String = "Interior Protection Coat"
    Const INTERIOR5_String As String = "Scotchgard"
    Const EXTERIOR1_String As String = "Hand Wash"
    Const EXTERIOR2_String As String = "Hand Wax"
    Const EXTERIOR3_String As String = "Check Engine Fluids"
    Const EXTERIOR4_String As String = "Detail Under Carriage"




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub packageListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles packageListBox.SelectedIndexChanged
        With Me
            'Clear the lists used for display
            .interiorListBox.Items.Clear()
            .exteriorListBox.Items.Clear()


            'Display the options in a Standard Detail Package

            If packageListBox.SelectedIndex = 0 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)

                'Display the options in a Deluxe Detail Package              
            ElseIf packageListBox.SelectedIndex = 1 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)

                'Display the options in a Luxury Detail Package
            ElseIf packageListBox.SelectedIndex = 2 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .interiorListBox.Items.Add(INTERIOR3_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR3_String)

                'Display the options in an Executive Detail Package
            ElseIf packageListBox.SelectedIndex = 3 Then
                .interiorListBox.Items.Add(INTERIOR1_String)
                .interiorListBox.Items.Add(INTERIOR2_String)
                .interiorListBox.Items.Add(INTERIOR3_String)
                .interiorListBox.Items.Add(INTERIOR4_String)
                .exteriorListBox.Items.Add(EXTERIOR1_String)
                .exteriorListBox.Items.Add(EXTERIOR2_String)
                .exteriorListBox.Items.Add(EXTERIOR3_String)
                .exteriorListBox.Items.Add(EXTERIOR4_String)

            End If

        End With
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'End the program

        Me.Close()
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        'Clear the fragrance list.
        fragranceComboBox.SelectedIndex = -1


        interiorListBox.Items.Clear()
        exteriorListBox.Items.Clear()

    End Sub

    Private Sub printOrderPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printOrderPrintDocument.PrintPage

        Dim printFont As New Font("Arial", 12)
        Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
        Dim lineHeightSingle As Single = printFont.GetHeight + 2
        Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left
        Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
        Dim printLineString As String
        Dim listIndexInteger As Integer




        'Set up and display heading lines.
        printLineString = "Valley Boulevard Auto Center"
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        printLineString = "Programmed by: Sandra Smith"
        verticalPrintLocationSingle += lineHeightSingle
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        'Leave a blank line etween the heading and detail line.
        verticalPrintLocationSingle += lineHeightSingle * 2
        'Set up the selected line.
        printLineString = "Your Package includes:" & "Exterior: "
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        For counter = 0 To exteriorlistbox.Items.Count - 1
            verticalPrintLocationSingle += lineHeightSingle * 2
            printLineString = Me.exteriorlistbox.Items.Item(counter)
            e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        Next

        verticalPrintLocationSingle += lineHeightSingle * 2
        printLineString = "Interior: "
        e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        verticalPrintLocationSingle += lineHeightSingle * 2
        For counter = 0 To interiorlistbox.Items.Count - 1
            verticalPrintLocationSingle += lineHeightSingle * 2
            printLineString = Me.interiorlistbox.Items.Item(counter)
            e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
        Next

        verticalPrintLocationSingle += lineHeightSingle * 2
        printLineString = "Fragrance: " & Me.fragrancecombobox.Text
        'Send the line to the graphics page oject.
        e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        'Loop through the entire list.
        For listIndexInteger = 0 To Me.fragrancecombobox.Items.Count - 1
            'Increment the Y position for the next line.
            verticalPrintLocationSingle += lineHeightSingle

            'Set up a line.
            printLineString = Me.fragrancecombobox.Items(listIndexInteger).ToString()

            'Send the line to the graphics page object.
            e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        Next listIndexInteger
    End Sub

    Private Sub PrintOrderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintOrderToolStripMenuItem.Click


        printOrderPrintDocument.Print()
    End Sub
End Class

so do you're saying i need to print out a vertical line in between each of the statements? Also, the code is still automatically printing the document. How can i get it to show a print preview box before it prints?

so do you're saying i need to print out a vertical line in between each of the statements? 2) How can i get just the fragrance selection to print out? Speaking of which, the code is still automatically printing the document. How can i get it to show a print preview box before it prints?

You need to add verticalPrintLocationSingle += lineHeightSingle in between each new line. This will create a new line to print on.

To print the selected fragrance, take out the following for next loop. The printLineString = "Fragrance: " & Me.fragrancecombobox.Text line should take care of printing the fragrance.

For listIndexInteger = 0 To Me.fragrancecombobox.Items.Count - 1
            'Increment the Y position for the next line.
            verticalPrintLocationSingle += lineHeightSingle

            'Set up a line.
            printLineString = Me.fragrancecombobox.Items(listIndexInteger).ToString()

            'Send the line to the graphics page object.
            e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)

        Next listIndexInteger

As far as print preview... well, I've never done it in VB and don't know how... sorry... I'll see if I can dig anything up.

As far as print preview, there is a PrintPreviewDialog control.

I don't know how to use it yet, but I haven't looked at any documentation yet.

This article has been dead for over six months. Start a new discussion instead.