ListBox doesn't update contents

chevron

Member
Joined
Sep 21, 2010
Messages
8
Location
India
Programming Experience
Beginner
Hi everyone, I am facing a little problem in my program.
I have a form called 'Bookmarks' that loads a bookmark file that contains a single word
on one line. I have put buttons on the form for sorting this list, deleting items and changing the character case to lowercase, uppercase or Sentence Case.

Now, deleting and sorting the list works fine, but when I try to change character cases,
nothing happens. I started the debugger and found that my case changing code does work, but the listbox doesn't seem to update it's contents.

I tried listbox.Update() and listbox.Refresh() but that didn't work either.
Please help.

20v1clu.png


Here's my code for 'Set Case':

VB.NET:
    Private Sub setCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setCase.Click
        If bookCase = "default" Then Exit Sub
        If bookCase = "lower" Then
            For i = 0 To marksList.Items.Count - 1
                marksList.Items.Item(i) = LCase(marksList.Items.Item(i))
            Next
        ElseIf bookCase = "upper" Then
            For i = 0 To marksList.Items.Count - 1
                marksList.Items.Item(i) = UCase(marksList.Items.Item(i))
            Next
        ElseIf bookCase = "sentence" Then
            For i = 0 To marksList.Items.Count - 1
                Dim tmp As String = UCase(Mid(marksList.Items.Item(i), 1, 1)) + Mid(LCase(marksList.Items.Item(i)), 2)
                marksList.Items.Item(i) = tmp
            Next
        End If
    End Sub
 
Your code seems to change the values of the items in the collection, but it's not actually listing the changed items. You need to clear the listbox and use the markslist.Items.Add() method with the revised collection.
 
Have you tried saving (looking to see if the bookmark file took the lower/uppercase change) and then reloading the bookmark file that feeds the listbox control once the action is performed?
 
Thank you Solitaire! :)
I cleared the listbox after copying it's modified contents into a temporary list and added the contents of the list to the listbox
and it worked!

Here's the updated code for 'Set Code' if anyone else has the same problem:

VB.NET:
Private Sub setCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setCase.Click
        If bookCase = "default" Then Exit Sub
        Dim tmpList As String() = Nothing
        Array.Resize(tmpList, marksList.Items.Count)

        If bookCase = "lower" Then
            For i = 0 To marksList.Items.Count - 1
                tmpList(i) = LCase(marksList.Items.Item(i))
            Next
            marksList.Items.Clear()
            For i = 0 To tmpList.Count - 1
                marksList.Items.Add(tmpList(i))
            Next
        ElseIf bookCase = "upper" Then
            For i = 0 To marksList.Items.Count - 1
                tmpList(i) = UCase(marksList.Items.Item(i))
            Next
            marksList.Items.Clear()
            For i = 0 To tmpList.Count - 1
                marksList.Items.Add(tmpList(i))
            Next
        ElseIf bookCase = "sentence" Then
            For i = 0 To marksList.Items.Count - 1
                Dim tmp As String = UCase(Mid(marksList.Items.Item(i), 1, 1)) + Mid(LCase(marksList.Items.Item(i)), 2)
                tmpList(i) = tmp
            Next
            marksList.Items.Clear()

            For i = 0 To tmpList.Count - 1
                marksList.Items.Add(tmpList(i))
            Next
        End If
End Sub

@aphillaura2000: Yes, I've tried saving the list to a file. It does indeed modify the list's contents.

**thread solved**
 
Back
Top