C# Winforms - 更改列表视图项目的标签

发布于 2024-09-17 06:53:12 字数 954 浏览 1 评论 0原文

我查看了列表视图控件,但无法找到每个项目的标签是如何定义的。当我将图像加载到列表视图中时,我会在图像下方看到图像的完整路径。我想做的是将文本替换为对图像本身更具描述性的内容,而不是图像所在的路径。

如果有人能指出我正确的方向,我将不胜感激。

编辑为更具描述性:我拥有的是一个在启动时自动加载图像的程序,图像被加载到列表视图中并显示在大图标视图中。因此,当它们加载时,会显示图标,并且下面的文本(标签)显示图像的路径。如果我能让它只显示文件名,那就太棒了。所有这些都是以编程方式完成的。

private void Form1_Load(object sender, EventArgs e)
    {
        listView1.LargeImageList = imageList1;
        var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons");

        foreach (var item in files)
        {
            addImage(item);
        }

    }

    private void addImage(string imageToLoad)
    {
        if (imageToLoad != "")
        {
            imageList1.Images.Add(Image.FromFile(imageToLoad));
            listView1.BeginUpdate();
            listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);
            listView1.EndUpdate();

        }
    }

I have looked through the listview controls and wasnt able to find how the label for each item is defined. When I load an image into the listview, I see the full path of the image under the image. What Im looking to do is replace the text with something more descriptive of the image itself, and not the path where it is located.

If someone can point me in the right direction, I would be thankful.

Edit to be more descriptive: What I have is a program that automatically loads images when it starts, the images are loaded into a listview and shown in the Large Icon view. So when they are loaded, the icon displays, and the text(label) underneath shows the path of the image. If I can get it to display just the filename, that would be awesome. All this is done programmatically.

private void Form1_Load(object sender, EventArgs e)
    {
        listView1.LargeImageList = imageList1;
        var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons");

        foreach (var item in files)
        {
            addImage(item);
        }

    }

    private void addImage(string imageToLoad)
    {
        if (imageToLoad != "")
        {
            imageList1.Images.Add(Image.FromFile(imageToLoad));
            listView1.BeginUpdate();
            listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);
            listView1.EndUpdate();

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-09-24 06:53:12

我相信这只是 文本属性 ListViewItem。

编辑:

所以只需将这一行更改

listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);

为:

listView1.Items.Add(System.IO.Path.GetFileName(imageToLoad), imageList1.Images.Count - 1);

I believe it's just the Text Property of ListViewItem.

Edit:

So just change this line:

listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);

to

listView1.Items.Add(System.IO.Path.GetFileName(imageToLoad), imageList1.Images.Count - 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文