从列表视图运行文件

oizo666.

新成员
加入
2月26日,2021年
消息
3
编程经验
Beginner
大家好,

IM Tryng要使Smal程序从列表视图(当时一个)打开文件。
任何人都可以指向正确的方向或者可以帮助我一点吗?

我已经完成了使用OFD并获得路径方式的列表视图,但我不知道如何进一步获得。

最好的祝福
 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,698
地点
悉尼,澳大利亚
编程经验
10+
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.
 

oizo666.

新成员
加入
2月26日,2021年
消息
3
编程经验
Beginner
嘿jmplhinney,
感谢您的回复。我将来会尝试更具体地说。
我有一个按钮,将文件放在要列表中。和运行solidworks的按钮。
所以我的第一个问题是:
如何创建一个我可以在第二个按钮中使用的字符串打开该文件?我是一个真正的noob在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;           
            }
        }
    }
 
最后编辑了主持人:

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,833
地点
切萨皮克,va.
编程经验
10+
我不使用SolidWorks,也不知道它的自动化对象模型。 SolidWorks的文档如何说如何让它打开文件?
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,833
地点
切萨皮克,va.
编程经验
10+
If that's the case, the just take the selected item from the list view and pass it in as the 文件名 parameter of that method. Why would you need to create a new string?
 

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,698
地点
悉尼,澳大利亚
编程经验
10+
If that's the case, the just take the selected item from the list view and pass it in as the 文件名 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.
 
最佳 底部