Home > DeveloperSection > Forums > Refresh a listbox to display new values
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    September-04-2013 3:16 AM

 C# C# 
Ratings:


 1 Reply(s)
 1088  View(s)
Rate this:

Hi Guys

I'm filling out my text box with values Im grabbing on line and passing them to the listbox like so:

        // textBox1.Text = test.ToString();

        string[] names = result.Split('|');

        foreach (string name in names)

        {

            listBox1.Items.Add(name);

        }

However I'm trying to click on a folder and have the files displayed from there be shown in my listbox1. THis is what I've tried:

   using (var testy = new WebClient())

        {

            test = testy.DownloadString("http://server.foo.com/images/getDirectoryList.php?dir=test_folder");

            string[] names1 = test.Split('|');

            foreach (string name in names1)

            {

                listBox1.Items.Clear();

                listBox1.Items.Add(name);

                listBox1.Update();

            }

        }

But all that happens is that my listbox empties and doesn't get refreshed.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-04-2013 7:10 AM

Hi Royce,

Use a BindingSource

BindingSource bs = new BindingSource();

List<string> names1 = new List();

bs.DataSource = names1;

comboBox.DataSource = bs;

   using (var testy = new WebClient())

    {

        test = testy.DownloadString("http://server.foo.com/images/getDirectoryList.php?dir=test_folder");

        names1.AddRange(test.Split('|'));

        bs.ResetBindings(false);

    }

The BindingSource will take care of everything for you.


Don't want to miss updates? Please click the below button!

Follow MindStick