Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my text file connected to a checked listbox control. I can add and delete the different items that are inside. The problem is that when I delete the textfile connected to the Checkedlistbox does not update, I have to exit out and come back in to see the changes that I have made.

here is the delete code:

Dim List As New List(Of String)(File.ReadAllLines("Mytext"))

      For Each line In CheckedListBox1.CheckedItems

          'Remove the line to delete, e.g.
          List.Remove(line)

      Next

      File.WriteAllLines("Mytext", linesList.ToArray())


I need help with getting the data to refresh

What I have tried:

I have tried to do a clear and reread the data using the readalllines. I have also tried a refresh and update on the checkedlistbox.
Posted
Updated 7-Feb-17 17:20pm
Comments
[no name] 7-Feb-17 18:56pm    
"I have tried to do a clear and reread the data using the readalllines"... yes... AND?

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.
 
Share this answer
 
You can add a button to rebind the updated text file to the checkedlistbox, e.g.
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
        CheckedListBox1.Items.Clear()

        Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("TextFile1.txt")

            Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            Reader.Delimiters = New String() {","}

            Dim Row As String()

            While Not Reader.EndOfData
                Row = Reader.ReadFields()
                CheckedListBox1.Items.Add(Row(1))
            End While

        End Using

End Sub
 
Share this answer
 
v3
Comments
Member 11856456 8-Feb-17 8:16am    
I had to change the code just a little to run the string I already created. but the code works beautifully, thank you for your help.

While Not Reader.EndOfData
lines = Reader.ReadFields()
CheckedListBox1.Items.AddRange(lines)
End While

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900