Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listbox Refresh() in c#

int[] arr = int[100];
listBox1.DataSource = arr;
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
    .....//some processes
    listBox1.DataSource = null;
    listBox1.DataSource = arr;
}

is not working,

also,

listBox1.Refresh(); is not working,

also,

listBox1.Update(); is not working,


i know i can use BindingList<T> but i have to work with only array.

can you help me how can i refresh listbox?

like image 584
Okan Kocyigit Avatar asked Feb 27 '11 22:02

Okan Kocyigit


3 Answers

my first answer on stack exchange here.

C# .Net 4.0:

listBox1.DataSource = null;
listBox1.DataSource = names;

I noticed that setting the datasource for the first time, it refreshes. When it's set, and you try set it to the same one again, it doesn't update.

So I made it null, set it to the same one, and it displayed correctly for me with this issue.

like image 195
Grease Avatar answered Oct 07 '22 02:10

Grease


I inherited ListBox and added a public method calling RefreshItems() which does what we want. Already implemented and all. I dont know why they didnt put in a public method.

like image 43
gdube Avatar answered Oct 07 '22 02:10

gdube


ListBox only updates its shown content when the object that is binded on dataSource notifys it own changes. the BindingSource object has an event called DataSourceChanged. when the Source is changed to a different object the Listbox will update itself. Same thing when you bind a List. Nothing will happen if you change the List, because the List doesn't notify that it has been changed. There is a Simple solution for this Problem: use BindingList http://msdn.microsoft.com/de-de/library/ms132679%28v=vs.110%29.aspx

the BindingList has the ListChanged Event is called every time when the List is changed (obviously). So the DataBindings of Windows.Form objects use events like ListChanged to update themselves. A simple List doesn't support this event.

SO if you want to work with a lot of Data Bindings you should know about: http://msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

like image 43
user3803560 Avatar answered Oct 07 '22 02:10

user3803560