How to quickly select all items in a ListBox?

I have an ownerdrawn ListBox to bind a form (Windows Forms) to a data source (BindingList). I need to be given the opportunity to select all the items very quickly (up to 500,000).

This is what I am doing now:

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

This is incredibly slow and unacceptable. Does anyone know a better solution?

+5
source share
4 answers

Assuming this is a Windows Forms problem: Windows Forms will draw the changes after each selected item. To turn off drawing and turn it on after you finish, use the BeginUpdate() and EndUpdate() methods.

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

You can try listbox.SelectAll ();

The following is a link to the Microsoft documentation on ListBox SelectAll ():

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

+1
source

Found another way: "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)); 
0
source

Source: https://habr.com/ru/post/1241268/


All Articles