How to select all items in a ListBox really fast?

非 Y 不嫁゛ 提交于 2019-12-06 02:36:46

问题


I have an ownerdrawn ListBox on a form (Windows Forms) binding to a datasource (BindingList). I need to provide an option to select all items (up to 500000) really fast.

This is what I am currently doing:

for (int i = 0; i < listBox.Items.Count; i++)
    listBox.SetSelected(i, true);

This is incredibly slow and not acceptable. Does anybody know a better solution?


回答1:


Assuming this is a Windows Forms problem: Windows Forms will draw changes after each selected item. To disable drawing and enable it after you're done use the BeginUpdate() and EndUpdate() methods.

listBox.BeginUpdate();

for (int i = 0; i < listBox.Items.Count; i++)
    listBox.SetSelected(i, true);

listBox.EndUpdate();



回答2:


You can try listbox.SelectAll();

Following is link to microsoft documentation on ListBox SelectAll():

https://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectall(v=vs.110).aspx




回答3:


You can use SelectAll() method.

Listbox.SelectAll();

https://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectall(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2




回答4:


Found another way, that's "more faster":

[DllImport("user32.dll", EntryPoint = "SendMessage")]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);

// Select All
SendMessage(listBox.Handle, 0x185, (IntPtr)1, (IntPtr)(-1));

// Unselect All
SendMessage(listBox.Handle, 0x185, (IntPtr)0, (IntPtr)(-1));


来源:https://stackoverflow.com/questions/34953171/how-to-select-all-items-in-a-listbox-really-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!