Preventing ListBox scrolling to top when updated

前端 未结 3 1333
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 02:05

I\'m trying to build a simple music player with a ListBox playlist. When adding audio files to the playlist, it first fills the ListBox with the filenames and then (on a sep

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 02:23

    It might be prudent to disable any painting of the Listbox when doing the updates. Use ListBox.BeginUpdate() and then update the entries, when done, invoke ListBox.EndUpdate(). This should ensure that no refreshes occurs during the update.

    You should also ensure that you do not refresh the ListBox from a thread either as cross-threading is dis-allowed, and the runtime will barf with a message saying that 'Cross Threading Exception' has occurred.

    Use the ListBox's BeginInvoke(...) method to get around the cross threading issue. See here on the MSDN on how this is done.

提交回复
热议问题