Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 14-Aug-12 20:36pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^])
  Permalink  
v2
Comments
asdf9009 at 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ő at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.150301.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100