none
How do I clear the listbox in Visual Basic?

    Question

  • Here I HAVE MY VISUAL BASIC CODE. And, I would need you to help me clear the listbox for me. I am unable to do that and am getting anomalies when I use the SelectedIndexChanged event twice. It's all repeating. Please help me.

    Thank you very much. I appreciate it. This problem shouldn't be too hard to fix.

    Private Sub cboSearchCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSearchCustomer.SelectedIndexChanged
            Dim strFmt As String = "{0,13}{1,13}{2,13}{3,10}{4,13}"
            Dim strFmt2 As String = "{0,13}{1,12}{2,11}{3,8}{4,11}"
            Dim strConn As String
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEMP\RESORT.MDB"
            Dim strSql2 As String = "SELECT Customers.CustomerID, Customers.CustomerName, Customers.Membership, Reservations.CheckInDate, Reservations.Meal, Reservations.Kid, Reservations.Adult, Reservations.Days, [CheckInDate]+[Days] AS CheckOutDate FROM Customers INNER JOIN Reservations ON Customers.CustomerID = Reservations.CustomerID ORDER BY Customers.CustomerName"
            Dim dataAdapter2 As New OleDb.OleDbDataAdapter(strSql2, strConn)
            dataAdapter2.Fill(dt2)
            dataAdapter2.Dispose()
    
            Dim strEntry As String
            Dim strArray() As String
            Dim strID As String
            strEntry = cboSearchCustomer.SelectedItem.ToString()
            strArray = strEntry.Split(" "c) ' the c stands for character (split several variables into characters)
            strID = strArray(0)
            lstReservation.Items.Clear()
            lstReservation.Items.Add(String.Format(strFmt, "Check In", "Check out", "Meal Plan", "Adults", "Children"))
            lstReservation.Items.Add("")
            For intY = 0 To dt2.Rows.Count - 1
                If CStr(dt2.Rows(intY)("customerid")) = strID Then
                    lstReservation.Items.Add(String.Format(strFmt2, FormatDateTime(dt2.Rows(intY)("checkindate"), DateFormat.ShortDate), FormatDateTime(dt2.Rows(intY)("checkoutdate"), DateFormat.ShortDate), dt2.Rows(intY)("meal"), dt2.Rows(intY)("adult"), dt2.Rows(intY)("kid")))
                End If
            Next
        End Sub

    Here is what the problem looks like.

    Friday, February 05, 2010 2:54 AM

Answers

  • did you stop to think that maybe it is not the ListBox that is duplicating? Because you ARE clearing the items

    from what I can see you must be decalring your datatable outside of IndexChanged Sub.

    therefore every time the index changes, you are adding more data to the same table that is already populated.

    try moving your datatable declaration for dt2, ie Dim dt2 As DataTable = New DataTable within this Sub and see what happens
    • Marked as answer by antitru5t Friday, February 05, 2010 4:17 AM
    Friday, February 05, 2010 3:43 AM

All replies

  • listbox.Items.Clear
    Friday, February 05, 2010 3:11 AM
  • I have already cleared the box, but it still does not clear it.
    Friday, February 05, 2010 3:15 AM
  • My question is where should I put listbox.Items.Clear?
    Friday, February 05, 2010 3:18 AM
  • I guess I should read before wild guessing

    Perhaps it is in your query try adding a group by

    SELECT Reservations.CheckInDate, Reservations.Meal, Reservations.Kid, 
    	Reservations.Adult, [CheckInDate]+[Days] AS CheckOutDate 
    FROM Customers INNER JOIN Reservations ON Customers.CustomerID = Reservations.CustomerID 
    GROUP BYReservations.CheckInDate, Reservations.Meal, Reservations.Kid, 
    	Reservations.Adult, [CheckInDate]+[Days], Customers.CustomerName
    ORDER BY Customers.CustomerName
    • Edited by TechNoHick Friday, February 05, 2010 3:27 AM
    Friday, February 05, 2010 3:21 AM
  • Why do I need a group?
    Friday, February 05, 2010 3:23 AM
  • No, the databinding is not wrong.

    I just want to clear the box and allow new data to come in. 

    The text is what I wanted.
    Friday, February 05, 2010 3:30 AM
  • My question is where should I put listbox.Items.Clear?

    At the start of the sub, just after the DIM lines.

       lstReservation.Items.Clear()
    Friday, February 05, 2010 3:34 AM
  • Even worse.  Should I make any changes and additions so that it would work?
    Friday, February 05, 2010 3:36 AM
  • You are already clearing the list box at an appropriate point - just before you write the header.  The header is being written once only, so this means that the problem is in the query - the query is returning the results twice.  AFAICT there's nothing wrong with the way you are writing the results into the listbox.

    You could confirm this by inserting a breakpoint at the start of the loop and looking at the the datatable - I believe you will find the lines doubled up.  You need to examine the source data and the query to determine why you are getting those lines twice.
    Friday, February 05, 2010 3:42 AM
  • did you stop to think that maybe it is not the ListBox that is duplicating? Because you ARE clearing the items

    from what I can see you must be decalring your datatable outside of IndexChanged Sub.

    therefore every time the index changes, you are adding more data to the same table that is already populated.

    try moving your datatable declaration for dt2, ie Dim dt2 As DataTable = New DataTable within this Sub and see what happens
    • Marked as answer by antitru5t Friday, February 05, 2010 4:17 AM
    Friday, February 05, 2010 3:43 AM
  • Thanks all for helping me solve this problem. :)  You guys are the best.  To jwavila, you get the props.
    Friday, February 05, 2010 4:17 AM