ListBox Populating Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NvrBst

    #1

    ListBox Populating Question

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

    I've tried wrapping "Listbox.Suspen dLayout()" and
    "Listbox.Resume Layout()" 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
  • Marc Gravell

    #2
    Re: ListBox Populating Question

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

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

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

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

    That do?

    Marc

    Comment

    • NvrBst

      #3
      Re: ListBox Populating Question

      On Jan 15, 11:49 pm, Marc Gravell <marc.grav...@g mail.comwrote:
      Well, if you still a BindingSource between the ListBox and the
      BindingList<T>, then you can simply suspend the BindingSource:
      >
                  bindingSource1. DataSource = list;
                  listbox1.DataSo urce = bindingSource1;
                  // ...
                  bindingSource1. SuspendBinding( );
                  // ...
                  bindingSource1. ResumeBinding() ;
                  // (might also need bindingSource1. ResetBindings(f alse);
                  // but see how it goes without first)
      >
      Alternatively, you can just tell the list itself to shut up for a
      while:
      >
                  bool wasEnabled = list.RaiseListC hangedEvents;
                  list.RaiseListC hangedEvents = false;
                  try
                  {
                      // ...
                  }
                  finally
                  {
                      if (wasEnabled) // then re-enable it and force a
      refresh
                      {
                          list.RaiseListC hangedEvents = true;
                          list.ResetBindi ngs();
                      }
                  }
      >
      That do?
      >
      Marc
      Ahh, Thank you kindly. Both methods you mentioned works perfectly :)

      NB

      Comment

      Working...