locked
How to get selected item from ListView? RRS feed

  • Question

  •   Hello.How I can get the selected item from a listview.for example if I wan't to delete it.....

    2 - I have a Folder Browsing Dialog.How I can search file (.txt or .mp3) in the selected path?

    Saturday, June 24, 2006 7:15 AM

Answers

All replies

  • To get the selected item :
    ListView1.SelectedItems(0)
    And to remove the selected item :
    ListView1.Items.Remove(ListView1.SelectedItems(0))
    For the second question, take a look at this thread : Getting files with specific extensions using FolderBrowserDialog.

    This is a Visual Basic code.
    Saturday, June 24, 2006 7:39 AM
  • is not working

    Error:Error 1 'System.Windows.Forms.ListView.SelectedItems' is a 'property' but is used like a 'method' 

    Saturday, June 24, 2006 10:25 AM
  • You must use it as a property, for example :
    Me.Text = ListView1.SelectedItems(0).ToString
    • Proposed as answer by Cofz922 Monday, May 14, 2012 8:54 AM
    Saturday, June 24, 2006 10:43 AM
  • The second is not working....with remove method...
    Saturday, June 24, 2006 11:06 AM
  • Why?, remember, you must write ";".
    ListView1.Items.Remove(ListView1.SelectedItems(0));
    Saturday, June 24, 2006 11:47 AM
  •  

    is not working

    Error:Error 1 'System.Windows.Forms.ListView.SelectedItems' is a 'property' but is used like a 'method' 

    Saturday, June 24, 2006 11:48 AM
  • ok....

     is working...: ListView1.Selection[0] not ListView2.Selection(0)

    Saturday, June 24, 2006 11:52 AM
  •  try
                {
                    filePath = (dirInfo.ToString() + "\\" + listView1.SelectedItems[0].Text);
                    toolStripStatusLabel1.Text = filePath;
                    
                }
                catch (Exception) { }
    • Proposed as answer by pyosa11 Wednesday, September 16, 2009 7:09 PM
    Wednesday, September 16, 2009 7:08 PM
  • Try this, works perfectly fine.

    if (ListView1.SelectedItems.Count > 0)
          ListView1.SelectedItems[0].Remove();

    First line checks if anything is selected (instead of using a try block) the second line simply removes the item from you LISTVIEW.
    • Proposed as answer by cr0ck3t Monday, December 21, 2009 7:36 AM
    Thursday, September 17, 2009 9:05 AM
  • Be careful with having an empty catch... I was working on code once which was not acting as expected and finally got to the point where I was feeding it gibberish to try to make it fail... it turned out it was failing in a totally different object, but this object was catching the exception without informing me and carying bravely on :S
    Monday, December 21, 2009 7:42 AM
  •         Dim theSegment As ListView.SelectedListViewItemCollection
            theSegment = Me.ListView1.SelectedItems
           
            For Each item In theSegment
                Me.ListView1.Items.Remove(item)

            next

     

    • Proposed as answer by cwdurand Monday, July 5, 2010 12:15 AM
    Monday, July 5, 2010 12:13 AM
  • I think it may helpful.....

    if (lstview.SelectedIndex >= 0)
      {
         var selectedItems = lstview.SelectedItems;
         foreach (ClassName selectedItem in selectedItems)
           {
             lstview.Items.Remove(selectedItem);
             break;
           }            
       }



    Thanks Karthikeyan.D
    • Proposed as answer by Pramod Lawate Monday, April 9, 2012 6:20 AM
    Friday, February 18, 2011 2:06 PM
  • The reason the code was not originally working for him was because you provided Visual Basic code and not C# code.

    Using () in c# refers to methods. So to convert this from Visual Basic to C# just change the () to []

    Saturday, July 14, 2012 2:30 AM
  • Thank you so muchh!!!!!
    Friday, January 18, 2013 1:51 AM