简体   繁体   English

C# 列表框将文本转换为超链接

[英]C# Listbox convert text to hyperlink

I had a form that allow user to input the Name and perform search on various defined folders and display searched files (full path) in listbox.我有一个表单,允许用户输入名称并对各种定义的文件夹执行搜索,并在列表框中显示搜索到的文件(完整路径)。 Now I want to covert the "Full Path" string into hyperlink so when user click on it inside the ListBox, it will launch Window explorer to locate and highlight the file.现在我想将“完整路径”字符串转换为超链接,因此当用户在 ListBox 内单击它时,它将启动 Window 资源管理器来定位并突出显示文件。 Here is the code I got so far:这是我到目前为止得到的代码:

 public void searchfile()
    {
        
        string fn = txt_filename.Text;
        lst_box1.Items.Clear();

        try
        {
            // Only get files that begin with the letter "c".
            List<string> dirs = new List<string>();
            dirs.AddRange(Directory.GetFiles(@"D:\Temp\Downloads", "*" + fn + "*"));
            
            foreach (string dir in dirs)
            {
                
                    lst_box1.Items.Add(dir);
                   
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Thanks谢谢

Your question is about what to do within the visualization within WinForms.您的问题是关于在 WinForms 中的可视化中做什么。 But your code is about how you fill the ListBox with data.但是您的代码是关于如何用数据填充 ListBox。

The ListBox has an event SelectedIndexChanged . ListBox 有一个事件SelectedIndexChanged This will be called, whenever another item was selected within that control.每当在该控件中选择另一个项目时,都会调用它。

You could subscribe to it and take the selected item value to open an explorer window at the desired location.您可以订阅它并获取所选项目值以在所需位置打开资源管理器 window。

If you don't know, how to open a file explorer on a specific location with C#, take a look at this question .如果您不知道如何使用 C# 在特定位置打开文件资源管理器,请查看此问题

You could also open it by double-clicking:您也可以通过双击打开它:

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        var item = listBox1.SelectedItem;

        // open Explorer with the path
        string sFolder_Path = listBox1.SelectedItem.ToString();
        Process.Start(sFolder_Path);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM