ListBox Populating Question

N

NvrBst

I populate a ListBox with a LogFile that has about (~1000 lines). The
ListBox's datasource is a BindingList<string>. Whenever I add the
elements, with the datasource set, it takes about 2 mins.

I've tried wrapping "Listbox.SuspendLayout()" and
"Listbox.ResumeLayout()" around the for loop (that does the adding)
but it still takes about 2 mins. Only thing I can seem to do is set
the DataSource to null, add to the BindingList and then reset the
DataSource (this way the ListBox is populated in about 1 second or
less)... Which, I think, defeats the purpose of the BindingList?

How do I tell the ListBox (or the BindingList) to temporarly stop
updating its bindings? Then I can just call the ".ResetBindings()"
function after I'm done adding to the BindingList.

Thanks
NB
 
M

Marc Gravell

Well, if you still a BindingSource between the ListBox and the
BindingList<T>, then you can simply suspend the BindingSource:

bindingSource1.DataSource = list;
listbox1.DataSource = bindingSource1;
// ...
bindingSource1.SuspendBinding();
// ...
bindingSource1.ResumeBinding();
// (might also need bindingSource1.ResetBindings(false);
// but see how it goes without first)

Alternatively, you can just tell the list itself to shut up for a
while:

bool wasEnabled = list.RaiseListChangedEvents;
list.RaiseListChangedEvents = false;
try
{
// ...
}
finally
{
if (wasEnabled) // then re-enable it and force a
refresh
{
list.RaiseListChangedEvents = true;
list.ResetBindings();
}
}

That do?

Marc
 
N

NvrBst

Well, if you still a BindingSource between the ListBox and the
BindingList<T>, then you can simply suspend the BindingSource:

            bindingSource1.DataSource = list;
            listbox1.DataSource = bindingSource1;
            // ...
            bindingSource1.SuspendBinding();
            // ...
            bindingSource1.ResumeBinding();
            // (might also need bindingSource1.ResetBindings(false);
            // but see how it goes without first)

Alternatively, you can just tell the list itself to shut up for a
while:

            bool wasEnabled = list.RaiseListChangedEvents;
            list.RaiseListChangedEvents = false;
            try
            {
                // ...
            }
            finally
            {
                if (wasEnabled) // then re-enable it and force a
refresh
                {
                    list.RaiseListChangedEvents = true;
                    list.ResetBindings();
                }
            }

That do?

Marc

Ahh, Thank you kindly. Both methods you mentioned works perfectly :)

NB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top