【问题标题】:How to open a file by clicking on it in TreeView如何通过在 TreeView 中单击文件来打开文件
【发布时间】:2018-06-23 11:50:16
【问题描述】:

单击树中最低的子节点时如何打开文件(例如 PDF)。

我创建了一个由数据库文件自动填充的 TreeView 程序。

【问题讨论】:

  • 请添加确切的问题和您的代码而不是图像。
  • @adrianoromano 你好!如果您在此处找到有用的答案,请不要忘记接受并投票。这样做,您和他们都获得了良好的声誉,它可以帮助您的问题的未来访问者确定哪个答案是最合适/最有帮助的答案。如果您对此有任何疑问,请查看此链接:how-does-accepting-an-answer-work

标签: winforms treeview


【解决方案1】:

将路径关联到该节点,然后使用如下内容:

private void LinkClicked(object sender, LinkClickedEventArgs e)
{
    Process.Start(@".\" + "YOUR FILE NAME AND EXTENSION");
}

阅读:Process.Start

通过指定文档或应用程序的名称来启动进程资源 文件并将资源与新的 System.Diagnostics.Process 组件相关联。

【讨论】:

    【解决方案2】:

    你需要看TreeNode Class

    例子:

    private void Form1_Load(object sender, EventArgs e)
        {
            //
            // This is the first node in the view.
            //
            TreeNode treeNode = new TreeNode("Windows");
            treeView1.Nodes.Add(treeNode);
            //
            // Another node following the first node.
            //
            treeNode = new TreeNode("Linux");
            treeView1.Nodes.Add(treeNode);
            //
            // Create two child nodes and put them in an array.
            // ... Add the third node, and specify these as its children.
            //
            TreeNode node2 = new TreeNode("C#");
            TreeNode node3 = new TreeNode("VB.NET");
            TreeNode[] array = new TreeNode[] { node2, node3 };
            //
            // Final node.
            //
            treeNode = new TreeNode("Dot Net Perls", array);
            treeView1.Nodes.Add(treeNode);
        }
    

    虽然示例不是从DB 读取节点。您可以编写查询来获取节点。按照以下路径的步骤操作,它将引导您从头开始

    Create a TreeView from a Database in Windows Forms and C#

    示例来源:Tree View

    要在节点上打开文件,请单击查看

    public event TreeNodeMouseClickEventHandler NodeMouseClick

        // If a node is double-clicked, open the file indicated by the TreeNode.
        void treeView1_NodeMouseClick(object sender, 
                TreeNodeMouseClickEventArgs e)
        {
        try
        {
            System.Diagnostics.Process.Start(@"c:\" + e.Node.Text);//e.Node.Text contains fileName
        }
            // If the file is not found, handle the exception and inform the user.
        catch (System.ComponentModel.Win32Exception)
        {
            MessageBox.Show("File not found.");
        }
    

    【讨论】:

      猜你喜欢
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多