Run file from list view

oizo666

New member
Joined
Feb 26, 2021
Messages
3
Programming Experience
Beginner
Hello everyone,

Im tryng to make smal program that opens a file from a list view (one at the time).
can anyone point me at the right direction or maybe help me a little?

I have made a list view that use ofd and get the the path way but i dont know how to get further.

Best regards
 
This is a classic case of needing to break a problem down into smaller parts. Opening a file has exactly zero to do with ListView control. To open a file, you generally need the file path, which would be contained in a string. Where that string comes from is irrelevant. As for the ListView, you get a string from an item or subitem and what you do with that string is irrelevant. As such, you can tackle those two parts completely independently and that's exactly what you should do. If you have a problem getting text out of a ListView then you can ask us about that, showing us what you've done and exactly how and where it doesn't meet your expectations. Similarly, if you're having trouble opening a file then you can ask about that. Opening a file from a ListView isn't really a thing. You need to divide and conquer and you'll find that you can solve a lot more of your problems for yourself to begin with, then ask us more focused question about the actual issues you encounter.
 
Hey jmcilhinney,
Thanks for your reply. I will try to be more specific in the future.
I have a button that ads the files to list. and a button to run solidworks.
So my first question is:
How do I create a string that i can use in my second button to open that file? im a real noob at C#
C#:
        private void metroButton3_Click(object sender, EventArgs e)

        {
            listView.Items.Clear();
            OpenFileDialog ofd = new OpenFileDialog();
          // Filter by Word Documents
            ofd.Filter = "Solidworks Ritningar|*.SLDDRW";
            ofd.Multiselect = true;
            ofd.ShowDialog();
           {
                foreach (string f in ofd.FileNames)
                {
                    FileInfo fi = new FileInfo(f);
                    ListViewItem item = new ListViewItem(fi.Name);
                    item.SubItems.Add(fi.FullName);
                    listView.Items.Add(item);
                }
            }
        }
                private async void printButton3_ClickAsync(object sender, EventArgs e)
            {
            

                SwApp = await SolidWorksSingleton.getApplicationasync();
                SwApp.Visible = true;           
            }
        }
    }
 
Last edited by a moderator:
I don't use Solidworks, nor know anything about its automation object model. What does the documentation for Solidworks say about how to get it to open a file?
 
If that's the case, the just take the selected item from the list view and pass it in as the FileName parameter of that method. Why would you need to create a new string?
 
If that's the case, the just take the selected item from the list view and pass it in as the FileName parameter of that method.
To be more specific, the ListView control only has a SelectedItems property, so you would need to get the first item in that collection, or loop over the collection if you want multi-select. The item itself will be a ListViewItem object, so you get its Text property. You can find all that information and more in the documentation for the ListView class. It's not something people generally want to do because they want to do the sexy part, i.e. writing code, but you should ALWAYS read the relevant documentation before posting a question. If you're having an issue with any specific type or member, read the documentation for that type or member first, then post the question if you don't find what you need. We're always here to help but we should be a last resort.
 
Back
Top Bottom