Click here to Skip to main content
15,867,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how can i make the listbox to update automatically ?

example what i build is when form loads displays folder names in listbox
but if i delete folder from the folder it doesn't update it

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each folder As String In System.IO.Directory.GetDirectories(".\Created\")
            ListBox1.Items.Add(Path.GetFileName(folder))
            ListView1.Items.Add(Path.GetFileName(folder))
        Next
    End Sub


This is when form loads display folder names inside "Created" in listbox
How to make it with timer to update the listbox information?

What I have tried:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Listbox1.Refresh()
        Listbox1.Update()
    End Sub


Bouth not work at all..
Posted
Updated 26-Nov-19 0:47am

Don't use a timer. Use a FileSystemWatcher object (google it, or look for articles about it here on CP).

When a new folder or file is added or removed from the target folder, call a method that clears the listbox, re-scans the folder, and updates the listbox.
 
Share this answer
 
Your question is dreadfully low on information.

If this is a WinForms or web app, you probably have to re-query the database. If it's wpf, properly binding the listbox to the data should be sufficient.
 
Share this answer
 
Comments
diablo22 25-Nov-19 15:53pm    
No use databound or other just simple get stuff. Program is vb.net winform.
If i use the tick to use the same function it just add non stop the folder names unlimited. I need some result how to do this in better way
#realJSOP 25-Nov-19 19:12pm    
I have absolutely no idea what you're talking about.
diablo22 26-Nov-19 6:10am    
how to make with timer to automatically update listbox1 items inside?

For example in Listbox1 has 3 items (3 folders that are inside folder Created)
- Test1
- Test2
- Test3
this is displayed in listbox1 when form opens (get count of folders and their names and put it in listbox)

If I go to folder Created> and delete folder Test1
With timer inside the form to check does it has something else (to update the listbox items)
So it will check that now it has 2 folders inside Created, and will update Listbox1 with items
- Test2
- Test3
#realJSOP 26-Nov-19 6:45am    
That's a COMPLETELY different question than what you originally asked.See my new solution

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