Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all ,
if an item in the listview is seleced then to know if any item is selected ..
I tried using listview1.SelectedItems.Count and listview1.SelectedIndices.Count but everytime it is giving count to 0 after selecting an item ...

thnx ,
Posted
Updated 21-Oct-19 9:54am

You can do something like this:

C#
foreach listview itm yourListView.Items)
{
   if(itm.selected)
   {
    Add in a string, its id or name or whatever
   }
}
 
Share this answer
 
v2
Comments
iampradeepsharma 18-Nov-11 2:05am    
Thanks for answering but there is no such property like selected on the item of the listview ...
[no name] 18-Nov-11 2:15am    
are you doing the same with javascript?Is should work in javascript, if you are doing this in c# then do something like this :
String strItem;
foreach(Object selecteditem in listBox1.SelectedItems)
{
strItem = selecteditem as String;
System.Diagnostics.Debug.WriteLine(strItem);
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
Label1.Text = li.Text.ToString();
}
}
}

label1 will display the selected item
 
Share this answer
 
Assume the ListBox selection mode is MultiSimple or MultiExtended: otherwise, of course, this makes no sense :)

Define a new SelectedObjectCollection, then initialize it in your Form Load Event:
C#
private ListBox.SelectedObjectCollection ListBoxSelections;

private void Form1_Load(object sender, EventArgs e)
{
    // initialize it here because prior to Form Load
    // a reference to the instance of listBox1 does
    // not exist to pass as a parameter

    // why initialize it ?
    // so we don't have to check if it's == null
    // when no items are selected and we want to
    // do something like access the 'Count property
    ListBoxSelections = new ListBox.SelectedObjectCollection(listBox1);
}
Then, 'wire-up' the SelectedIndexChanged Event to your ListBox:
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListBoxSelections = listBox1.SelectedItems;
}
And now any time you need to do whatever with the selected items in the ListBox, you can just use 'ListBoxSelections: if its 'Count property == #0, you know there are no current items selected.
 
Share this answer
 
v3
// Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
   {
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         listBox1.SetSelected(x,false);      
      else
         // Select all items that are not selected.
         listBox1.SetSelected(x,true);
   }
 
Share this answer
 
Comments
Kats2512 22-Oct-19 2:32am    
an unexplained code dump to such an OLD POST isn't going to help much.

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