424,054 Members | 1,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,054 IT Pros & Developers. It's quick & easy.

find index of multiple select items in listbox

100+
P: 375
hello
I am working in asp.net and C#
I am selecting multiple items in a list box.
Now I want a count of the seleted items also the index of the selected item

I browsed and found something called
" selectedindices.count "
but unfourtunately when i type listbox1 the selectedindices does not popup.

Any help please
regards
cmrhema
Sep 6 '07 #1
Share this Question
Share on Google+
10 Replies


Plater
Expert 5K+
P: 7,872
As per http://msdn2.microsoft.com/en-us/lib...es(VS.71).aspx
ListBox.SelectedIndices is a property in .net1.1.
My guess is you were just spelling wrong or not having correct capitalization.
Sep 6 '07 #2

100+
P: 375
As per http://msdn2.microsoft.com/en-us/lib...es(VS.71).aspx
ListBox.SelectedIndices is a property in .net1.1.
My guess is you were just spelling wrong or not having correct capitalization.
Thanks Plater for the reply
But I am using asp.net 2.0 and visual c# 2005.
and I did write ListBox1. but all i could trace was
selectedindex
selectedindexchanged
selectedvalue
selecteditem
selectionmode


selectedindices seems nowhere in the list

thanks
cmrhema
Sep 7 '07 #3

Shashi Sadasivan
Expert 100+
P: 1,435
Thanks Plater for the reply
But I am using asp.net 2.0 and visual c# 2005.
and I did write ListBox1. but all i could trace was
selectedindex
selectedindexchanged
selectedvalue
selecteditem
selectionmode


selectedindices seems nowhere in the list

thanks
cmrhema
Hello,
SelectedIndices appears only in windows application environment.
For asp .net (i tried this myself) iterate through each item of the list box
and check if their selected propert is true or not.

cheers
Sep 7 '07 #4

vanc
Expert 100+
P: 211
If you want to show number of selected items in the ListBox, then use ListBox.SelectedItems.Count
If you want to show indexes of selected items then use ListBox.Items.IndexOf(object as Item)

for eg: foreach(object o in ListBox.SelectedItems)
Display(ListBox.Items.IndexOf(o))

cheers.
Sep 7 '07 #5

100+
P: 375
If you want to show number of selected items in the ListBox, then use ListBox.SelectedItems.Count
If you want to show indexes of selected items then use ListBox.Items.IndexOf(object as Item)

for eg: foreach(object o in ListBox.SelectedItems)
Display(ListBox.Items.IndexOf(o))

cheers.
Sadha Shivam is right
Even SelectedItems does not appear.
Sep 7 '07 #6

Shashi Sadasivan
Expert 100+
P: 1,435
Sadha Shivam is right
Even SelectedItems does not appear.
Hi,
Since you are using the web controls over the form controls, they fall under 2 seperate namespaces, and are 2 different objects.
the listBox of windows forms falls in the namespace System.Windows.Forms
Windows forl ListBox Properties
and that of the web control under System.Web.UI.WebControls
windows web UI listbox properties
So please see the specific control that you are looking for and its functionality. Unfortunately you can use a windows form control in a asp .net (unless i am wrong)

cheers,
And you may rather call me Shashi :)
Sep 7 '07 #7

Plater
Expert 5K+
P: 7,872
That was my fault, for some reason I thought you had said .net1.1 for windows, and not the asp side.
sorry.
Sep 7 '07 #8

100+
P: 375
That was my fault, for some reason I thought you had said .net1.1 for windows, and not the asp side.
sorry.
No problem plater
This is my code


Expand|Select|Wrap|Line Numbers
  1. int listcount=0;
  2.  
  3.         for (int i = 0; i < ListBox1.Items.Count; i++)
  4.         {
  5.             if (ListBox1.Items[i].Selected == true)
  6.  
  7.                 listcount = listcount + 1;
  8.  
  9.         }
Now I want to find the values that were selected
Any help
Sep 8 '07 #9

Shashi Sadasivan
Expert 100+
P: 1,435
Expand|Select|Wrap|Line Numbers
  1. int listcount=0;
  2.         List<string> selectedItems = new List<string>();
  3.         for (int i = 0; i < ListBox1.Items.Count; i++)
  4.         {
  5.             if (ListBox1.Items[i].Selected == true)
  6.             { 
  7.                 listcount = listcount + 1;
  8.                 selectedItems.Add(ListBox1.Items[i]);
  9.              }   
  10.         }
and obviously seletedItems is the List which contains all the selected items
Sep 9 '07 #10

Plater
Expert 5K+
P: 7,872
If you use a collection object like List<>, you shouldn't need another varriable to count like "listcount" as you should be able to say selectedItems.Count
Expand|Select|Wrap|Line Numbers
  1. int listcount=0;
  2.         List<string> selectedItems = new List<string>();
  3.         for (int i = 0; i < ListBox1.Items.Count; i++)
  4.         {
  5.             if (ListBox1.Items[i].Selected == true)
  6.             { 
  7.                 listcount = listcount + 1;
  8.                 selectedItems.Add(ListBox1.Items[i]);
  9.              }   
  10.         }
and obviously seletedItems is the List which contains all the selected items
Sep 10 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.