Getting a value from a listview
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Getting a value from a listview

  1. #1
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Getting a value from a listview

    When some one clicks on a item in the listview. How can I get the value of that.

    here's my code

    Code:
    string fname = GetFileNameWithoutExtension(m_Playable.FullRowSelect);
                //I need to get the file name's Name. Not the ext.
                
                label3.Show();
                label3.Text = "Selected File Name : " + fname + ".bms";
                
                m_ListRealted.Items.Clear();
    
                string path = textBox2.Text;
                DirectoryInfo dr = new DirectoryInfo(@path);
                
                List<string> relatedfiles = new List<string>();
    
                foreach (FileInfo related in dr.GetFiles(fname + ".*"))
                {
                    
                    relatedfiles.Add(related.Name.ToString());
    
                }
    
            }
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Getting a value from a listview

    The ListView class has a SelectedItems and SelectedIndices property.

  3. #3
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Ok, can you tell me how to use them., This is the 1st time I have done any thing like this before. So a from the code above would help

    Thanks
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Getting a value from a listview

    Well, the code you have posted deals with loading items into a ListBox, not getting an item from it. You can just handle the SelectedIndexChanged event, get the selected items through the SelectedItems property and then do whatever you need to do with them:

    Code:
            void listView1_SelectedIndexChanged( object sender, EventArgs e )
            {
                ListView.SelectedListViewItemCollection items = listView1.SelectedItems;
                foreach ( ListViewItem item in items )
                {
                    // so something with each item
                }
            }

  5. #5
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Ok, sorry thats not working. Let me see if I can explain it better

    I have a list in a listview. A llist of files with a bms ext. They All have the same ext. When I click on one. I want it to list any related files with the same file name. It's just that the ext are different.
    Lets start with some thing simple

    Code:
    ListView.SelectedListViewItemCollection fname = listView1.SelectedItems;
    
                label3.Show();
                label3.Text = "Selected File Name : " + fname + ".bms"; //This is the selected file
    All I want it to do is display the filenames name (No Ext);
    Then I can do a match in the folder for any files with that same filename.*
    Last edited by bigjoe11a; August 2nd, 2009 at 06:03 PM.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Getting a value from a listview

    Quote Originally Posted by bigjoe11a View Post
    Ok, sorry thats not working. Let me see if I can explain it better

    I have a list in a listview. A llist of files with a bms ext.
    No, you have a list of strings, but I know what you are getting at.

    They All have the same ext. When I click on one. I want it to list any related files with the same file name. It's just that the ext are different.
    In what directory? How do I know where to search?
    If you have a directory, you can use the GetFiles method to get files that match a pattern, but you already know that. So, use the code I have already posted to loop through the selected items, get the directory, search in that directory, populate the other list.

  7. #7
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Yes I know all that. I just can't list the related files for the one I picked in the 1st listview.
    Thats why I need to know how to return the value from the selected item in the 1st listview. When I have that I can use it to select the related files for the one I selected.

    heres the code I can for when then form loads. This puts all the files with a bms ext in the 1st listview.
    Code:
     private void MapBase_Load(object sender, EventArgs e)
            {
                textBox2.Text = MLHSetting.Default.PlayableMaps;
                string path = textBox2.Text.TrimEnd();
                DirectoryInfo dr = new DirectoryInfo(@path);
    
                List<string> files = new List<string>();
                int totalfiles = 1;
                foreach (FileInfo file in dr.GetFiles("*.bms"))
                {
                    //add each file to the Generic list
                    files.Add(file.Name);
                    m_Playable.Items.Add(file.Name);
                    totalfiles += 1;
                }
                label2.Text = "Total Files : " + totalfiles.ToString();
    
            }
    Then when I select the file name from the list above. The code below will select and add the related files to the 2nd listview
    Code:
     private void m_Playable_SelectedIndexChanged(object sender, EventArgs e)
            {
                //string fname = GetFileNameWithoutExtension(m_Playable.SelectedItems);
                //string fname = m_Playable.SelectedItems(m_Playable.SelectedItems);
                ListView.SelectedListViewItemCollection items = m_Playable.SelectedItems;
    
                label3.Show();
                label3.Text = "Selected File Name : " + items + ".bms";
    
                m_ListRealted.Items.Clear();
    
                string path = textBox2.Text;
                DirectoryInfo dr = new DirectoryInfo(@path);
    
                List<string> relatedfiles = new List<string>();
    
                foreach (FileInfo related in dr.GetFiles(items + ".*"))
                {
    
                    relatedfiles.Add(related.Name.ToString());
    
                }
    Does that help
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,361

    Re: Getting a value from a listview

    Quote Originally Posted by bigjoe11a View Post
    Yes I know all that. I just can't list the related files for the one I picked in the 1st listview.
    Thats why I need to know how to return the value from the selected item in the 1st listview. When I have that I can use it to select the related files for the one I selected.

    heres the code I can for when then form loads. This puts all the files with a bms ext in the 1st listview.
    Code:
     private void MapBase_Load(object sender, EventArgs e)
            {
                textBox2.Text = MLHSetting.Default.PlayableMaps;
                string path = textBox2.Text.TrimEnd();
                DirectoryInfo dr = new DirectoryInfo(@path);
     
                List<string> files = new List<string>();
                int totalfiles = 1;
                foreach (FileInfo file in dr.GetFiles("*.bms"))
                {
                    //add each file to the Generic list
                    files.Add(file.Name);
                    m_Playable.Items.Add(file.Name);
                    totalfiles += 1;
                }
                label2.Text = "Total Files : " + totalfiles.ToString();
     
            }
    Then when I select the file name from the list above. The code below will select and add the related files to the 2nd listview
    Code:
     private void m_Playable_SelectedIndexChanged(object sender, EventArgs e)
            {
                //string fname = GetFileNameWithoutExtension(m_Playable.SelectedItems);
                //string fname = m_Playable.SelectedItems(m_Playable.SelectedItems);
                ListView.SelectedListViewItemCollection items = m_Playable.SelectedItems;
     
                label3.Show();
                label3.Text = "Selected File Name : " + items + ".bms";
     
                m_ListRealted.Items.Clear();
     
                string path = textBox2.Text;
                DirectoryInfo dr = new DirectoryInfo(@path);
     
                List<string> relatedfiles = new List<string>();
     
                foreach (FileInfo related in dr.GetFiles(items + ".*"))
                {
     
                    relatedfiles.Add(related.Name.ToString());
     
                }
    Does that help
    What would help is if you set a breakpoint on line highlighted line and look at the values in a debugger. When you do that, is it the expected value(s)? Does it makes sense to use this value/these values in a GetFiles(...) method call?

    If you aren't using the debugger or don't know how please let us know so we can help you. Guessing by punching in values isn't going to get you far (and will be frustrating to boot).

  9. #9
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Thanks Arjay. Like I said above that line doesn't work all so. In the label3.Text is just displays a long line System.Windows.forms.Listview + ListView.SelectedListViewItemCollection + .bms

    ListView.SelectedListViewItemCollection items = m_Playable.SelectedItems;

    And as for using bebug. Arjay I still don't know what t look for or what I'm looking at. I will try again

    All I'm trying to do is to get it to display the filename of what I selected so I can display any related files for the one I selected.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  10. #10
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Just wanted to let you know that this line is what it passes and its wrong

    ListView.SelectedListViewItemCollection = System.Windows.Forms.ListView.SelectedListViewItemCollection
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,361

    Re: Getting a value from a listview

    When you debug, you look at the values of the variables. Set a break point on the line I showed you with F9. Then start a debugging session by pressing F5. When the program opens, navigate through until you hit the break point. Step over (F10) each line of code until you get to the following:

    Code:
     
    DirectoryInfo dr = new DirectoryInfo(@path);
     
    List<string> relatedfiles = new List<string>();
     
    foreach (FileInfo related in dr.GetFiles(items + ".*"))
    The two variables of interest are the "@path" variable and the "items" variable. What we are looking for is for the contents of these variables to contain type and data that is valid for the particular method being called.

    For example, does "@path" contain a properly formed directory path (e.g. "D:\Program Files") that will work in a DirectoryInfo(...) constructor? If not, then you need to back up in the program (by stopping the debugging session and set a break point on the line where "@path" has been set) and figure out why "@path" doesn't contain the correct value.

    Similarly, the dr.GetFiles(...) method. This method requires a search filter (like "notep*.exe"). You are passing an "items" collection variable and appending ".*" That will probably give you the generic name of collection with a ".*" appended. Probably something like "SelectedListViewItemCollection.*" That probably isn't going to be the search filter you are looking for.

    Understand my goal here isn't to point you to the specific problem. Instead, it's to teach you how to you the debugger and think about the problem so you can figure out the issues on your own.

  12. #12
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Yes I know Arjay and I thanks you for it. Just until I can under stand what I doing and what I'm looking for when I use the debugger I'm stuck. How ever I did find this and it works.

    Code:
    string fname = Path.GetFileNameWithoutExtension(m_Playable.SelectedItems[0].Text);
    this just returns the file name and not the ext of the file. and it works. It does just what I wanted it to do.

    And again thanks for your help
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  13. #13
    Join Date
    Jul 2006
    Posts
    297

    Re: Getting a value from a listview

    Learning how to use the debugger is one of the best things you can do. Once you can see the values of the objects you're using everything makes more sense. Probably one of the best things I did was learn to use the debugger properly, defiantly worth the time spent learning to use it.

  14. #14
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Getting a value from a listview

    Thanks. That's what Arjay has been doing. And I'm trying it now again. I ran into another problem, Any way Arjay has been trying to teach me. One one these days I'll find some one that can teach me how to debug my programs.

    Thanks Guys for every thing
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center