Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im trying to select all items in a listbox by clicking a "Select all" button,
and i got some info from MSDN website,
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectall.aspx[^]
However when i try to use "Select All" method,it shows that such method could not be found.

I had already import the reference PresentationFramework.dll to my solution.
Posted

1 solution

Wait! You have confused everything! This has nothing to do with VisualStudio. And either you use WPF or WindowsForms control. PresentationFramework.dll is WPF, the link is for the WindowsForms control.
To allow user to select all elements in wpf listbox look here: http://stackoverflow.com/questions/1030276/wpf-listbox-and-select-all[^]
If you want to select programatically all elements, you can use the SelectedItems[^] property, and add all elements from the bound source (don't forget to set multiple selection mode[^])
 
Share this answer
 
v2
Comments
asdf9009 15-Aug-12 2:02am    
opps!!
thanks for telling me,
i was doing a project in Windows Forms!
So, can i get"SelectAll" method in Windows Forms environment?
Zoltán Zörgő 15-Aug-12 2:17am    
The standard control does not have such method, only the wpf control. Use this code line:
for(int i=0; i<listBox1.Items.Count; i++) listBox1.SetSelected(i,true);
And don't forget to set SelectionMode.

You can also make an extension method, if you want to use it many times:

using System;
using System.Windows.Forms;

namespace my
{
public static class ListBoxExtensionMethods
{
public static void SelectAll(this ListBox listbox, bool selected = true)
{
for(int i=0; i<listbox.Items.Count; i++) listbox.SetSelected(i,selected);
}

public static void DeselectAll(this ListBox listbox)
{
SelectAll(listbox, false);
}
}
}

asdf9009 15-Aug-12 17:23pm    
thanks...it works now...more to learn...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900