Reputation: 907
This is a newb question so I'm sorry.
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. How can I achieve what I want to do?
Upvotes: 1
Views: 5522
Reputation: 2811
This is the best way IMO (no flickers) You need to have a display member.
listBox1.BeginUpdate();
listBox1.DisplayMember = null;
listBox1.DisplayMember = "Display";
listBox1.EndUpdate();
Upvotes: 0
Reputation: 13599
before you do anything else remove the clear and update from the foreach
listBox1.Items.Clear();
foreach (string name in names1)
{
listBox1.Items.Add(name);
}
listBox1.Update();
Upvotes: 2
Reputation: 113
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.
Upvotes: 0
Reputation: 11597
your lines
foreach (string name in names1)
{
listBox1.Items.Clear();
listBox1.Items.Add(name);
listBox1.Update();
}
makes it that for every string you are removing ever other item in the list.
i'm pretty sure that's not what you want
Upvotes: 0