I am creating a program that make a person select different options and in other boxes it will populate the choice the person makes. Example is the program is fora car wash and if you choose standard or deluxe it will show in the other boxes what comes with that package.I got the program to work except when going to select a different option it still keeps the old info and displays the new info as well. I tried like at the bottom of the procedure Exteriorlist.Items.clear() and it would clear and never display the info. I am still working on the project I am trying to get the print option working properly I am just needing help in figuring out how to clear the info when I select a different package.

Public Class CarWash7

    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 Engine Compartment"
    Const EXTERIOR5_String As String = "Detail Under Carriage"


    Const Interior1_String As String = "Fragrance"
    Const Interior2_String As String = "Shampoo Carpets"
    Const Interior3_String As String = "Interior Protecttion Coat"
    Const Interior4_String As String = "Shampoo Upholstery"
    Const Interior5_String As String = "Scotchgard"



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

    End Sub
    'Clears All the boxes.
    Private Sub CleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CleToolStripMenuItem.Click
        'DetailPackageList.Items.Clear()
        Exteriorlist.Items.Clear()
        Interiorlist.Items.Clear()
        frangracecombolist.Items.Clear()

    End Sub

    Private Sub DetailPackageList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailPackageList.SelectedIndexChanged
        'Display the options in a Standard Detail Package
        If DetailPackageList.SelectedIndex = 0 Then
            Exteriorlist.Items.Add(EXTERIOR1_String)
            Interiorlist.Items.Add(Interior1_String)
            DetailPackageList.Items.Remove(Exteriorlist.Text)

            'Display the options in a Deluxe Detail Package
        ElseIf DetailPackageList.SelectedIndex = 1 Then
            Exteriorlist.Items.Add(EXTERIOR1_String)
            Exteriorlist.Items.Add(EXTERIOR2_String)
            Interiorlist.Items.Add(Interior1_String)
            Interiorlist.Items.Add(Interior2_String)
        End If


        'Display the options in a Executive Detail Package
        If DetailPackageList.SelectedIndex = 2 Then
            Exteriorlist.Items.Add(EXTERIOR1_String)
            Exteriorlist.Items.Add(EXTERIOR2_String)
            Exteriorlist.Items.Add(EXTERIOR3_String)
            Interiorlist.Items.Add(Interior1_String)
            Interiorlist.Items.Add(Interior2_String)
            Interiorlist.Items.Add(Interior3_String)
            'Display the options in a Luxury Detail Package
        ElseIf DetailPackageList.SelectedIndex = 3 Then
            Exteriorlist.Items.Add(EXTERIOR1_String)
            Exteriorlist.Items.Add(EXTERIOR2_String)
            Exteriorlist.Items.Add(EXTERIOR3_String)
            Exteriorlist.Items.Add(EXTERIOR4_String)
            Exteriorlist.Items.Add(EXTERIOR5_String)
            Interiorlist.Items.Add(Interior1_String)
            Interiorlist.Items.Add(Interior2_String)
            Interiorlist.Items.Add(Interior4_String)
            Interiorlist.Items.Add(Interior5_String)


        End If
        

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        'Set up the printer output
        Dim PrintFont As New Font("Arial", 12)
        Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
        Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Left
        Dim HorizontalPrintLocationSingle As Single = e.MarginBounds.Top
        Dim PrintLineString As String

        Using HeadingFont As New Font("Arial", 14)
            'Print Report Headings
            PrintLineString = "Very Busy Auto Center"
            e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, VerticalPrintLocationSingle, HorizontalPrintLocationSingle)
            HorizontalPrintLocationSingle += LineHeightSingle
        End Using

        'Print the programmer's name
        PrintLineString = "Programmer: A. Programmer"
        e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, VerticalPrintLocationSingle, HorizontalPrintLocationSingle)
        HorizontalPrintLocationSingle += LineHeightSingle * 2
        'Print the exterior data
        PrintLineString = "Exterior: "
        e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, VerticalPrintLocationSingle, HorizontalPrintLocationSingle)
        HorizontalPrintLocationSingle += LineHeightSingle
        For ListIndexInteger As Integer = 0 To Exteriorlist.Items.Count - 1
            PrintLineString = Exteriorlist.Items(ListIndexInteger).ToString
            e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, VerticalPrintLocationSingle, HorizontalPrintLocationSingle)
            HorizontalPrintLocationSingle += LineHeightSingle
        Next ListIndexInteger
        HorizontalPrintLocationSingle += LineHeightSingle 'Blank line
    End Sub

 
End Class

Dani AI

Generated

The symptom described by comes from not clearing the target lists before repopulating them (and from an unintended removal line that targets the package list). As suggested, the clears must run at the top of the SelectedIndexChanged handler — clearing after adding either leaves old items mixed with new ones or erases everything. Also remove the line that calls DetailPackageList.Items.Remove(...) — that modifies the package choices and is not needed to refresh the exterior/interior lists.

A concise, safe pattern (clear first, then add) — and how to populate a fragrance ComboBox for package-specific choices (answering ) — looks like this:

Private Sub DetailPackageList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
    Handles DetailPackageList.SelectedIndexChanged

    ' Clear previous values first
    Exteriorlist.Items.Clear()
    Interiorlist.Items.Clear()
    frangracecombolist.Items.Clear()   ' consider renaming to "fragranceComboBox" for clarity

    If DetailPackageList.SelectedItem Is Nothing Then Exit Sub
    Dim pkg As String = DetailPackageList.SelectedItem.ToString()

    Select Case pkg
        Case "Standard"
            Exteriorlist.Items.AddRange(New Object() {"Hand Wash"})
            Interiorlist.Items.AddRange(New Object() {"Fragrance"})
            frangracecombolist.Items.AddRange(New Object() {"Lemon", "New Car"})
        Case "Deluxe"
            Exteriorlist.Items.AddRange(New Object() {"Hand Wash", "Hand Wax"})
            Interiorlist.Items.AddRange(New Object() {"Fragrance", "Shampoo Carpets"})
            frangracecombolist.Items.AddRange(New Object() {"Citrus", "Lavender"})
        Case Else
            ' other packages
    End Select

    frangracecombolist.SelectedIndex = -1  ' no default scent selected
End Sub

For maintainability, replace hard-coded AddRange calls with a data structure (Dictionary or small Package class) mapping package names to exterior/interior/fragrance arrays and use AddRange on those arrays. Minor tips: wrap bulk updates in BeginUpdate/EndUpdate to avoid flicker, avoid manipulating the package list itself, and confirm the print routine is actually wired (the PrintPage handler must be attached via Handles PrintDocument1.PrintPage or AddHandler) so printing will work later.

Recommended Answers

All 2 Replies

If you haven't already figured this out, you need to copy lines 28 and 29 to just after line 34 so it clears the boxes before adding values. If this doesn't fix the problem, let me know.

What can you do if you need to add a dropdown menu to fragrance?

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.