CheckedListBox - FileInfo 对象的集合 - 显示自定义 ToString

发布于 2024-12-08 05:45:51 字数 481 浏览 2 评论 0原文

我在 WinForms 应用程序(3.5 运行时)中有一个 CheckedListBox,并且我将一堆 FileInfo 对象添加到 Items ObjectCollection 中。问题是我不喜欢 CheckedListBox 中显示的内容(因为 FileInfo 来自 Directory.GetFiles() 它只显示列表框中文件的 FileInfo.Name)。

有没有简单的方法可以更改 CheckedListBox 中显示的内容,而无需创建单独的自定义类/对象。

我基本上是在执行

checkedListBox.Items.Add(fileInfo)

,结果只是文件的文件名。

更改显示成员有效,但我无法创建自定义内容,只能创建 FileInfo 类中的现有属性。

我希望能够显示类似名称 - 全名

示例(所需)的内容: File1.txt - C:\Path\SubPath\File1.txt

I have a CheckedListBox in a WinForms app (3.5 runtime), and I am adding a bunch of FileInfo objects to the Items ObjectCollection. The problem is that I don't like what is displayed in the CheckedListBox (since the FileInfo was from a Directory.GetFiles() it just shows the FileInfo.Name of the file in the listbox).

Is there any easy way to change what is displayed in the CheckedListBox without having to create a seperate custom class/object.

I am basically doing

checkedListBox.Items.Add(fileInfo)

and the result is just the file name of the file.

Changing display member works but I can't create something custom, only the existing properties in the FileInfo class.

I want to be able to display something like Name - FullName

Example (desired):
File1.txt - C:\Path\SubPath\File1.txt

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

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

发布评论

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

评论(2

你是暖光i 2024-12-15 05:45:51

事实上,看起来应该是可能的。 CheckedListBox 有一个 FormattingEnabled 属性和一个继承自 ListBoxFormat 事件,该事件在显示每个项目之前调用。因此,沿着这些思路的东西应该有效:

myCheckedListBox.FormattingEnabled = true;
myCheckedListBox.Format += (s, e) => { e.Value = string.Format("{0} - {1}", ((FileInfo)e.ListItem).Name, ((FileInfo)e.ListItem).FullName); };

但还没有测试过。另请参阅 MSDN

旧回答:

我认为如果不创建包装器就无法做到这一点。虽然 10 行代码对我来说似乎并没有那么糟糕:

class FileInfoView
{
    public FileInfo Info { get; private set; }

    public FileInfoView(FileInfo info)
    {
        Info = info;
    }

    public override string ToString()
    {
        // return whatever you want here
    }
}

拥有视图模型的额外优势是您可以按照您喜欢的方式进一步装饰它以用于显示目的。

Actually, it seems like it should be possible after all. The CheckedListBox has a FormattingEnabled property and a Format event inherited from ListBox which is called before each item is displayed. So something along these lines should work:

myCheckedListBox.FormattingEnabled = true;
myCheckedListBox.Format += (s, e) => { e.Value = string.Format("{0} - {1}", ((FileInfo)e.ListItem).Name, ((FileInfo)e.ListItem).FullName); };

Haven't tested it though. See also MSDN

Old answer:

I don't think you can do it without creating a wrapper. Although 10 lines of code don't seem all that bad to me:

class FileInfoView
{
    public FileInfo Info { get; private set; }

    public FileInfoView(FileInfo info)
    {
        Info = info;
    }

    public override string ToString()
    {
        // return whatever you want here
    }
}

The additional advantage to having a view model is that you can decorate it further for display purposes all the way you like.

故事与诗 2024-12-15 05:45:51

我不知道除了创建一个 custom 类并在其中包含一个 FileInfo 实例之外是否有解决方法
通过这种方式,您可以创建一个新的属性并在其中包含自定义数据,或者覆盖ToString()函数,

例如 (这用于演示目的)

 MyFileInfo
    {
        public FileInfo TheFileInfo;
        public string CustomProperty
        {
           get
           {
               if(this.TheFileInfo != null)
                    return this.TheFileInfo.FileName + this.TheFileInfo.FullName;
                return string.Empty;
           }
        }
    }

I dont know if there is work around for this except creating a custom class and include an instance of FileInfo inside it
and in this way you either create a new property and include a custom data in it or override the ToString() function

something like (this for demonstration purposes)

 MyFileInfo
    {
        public FileInfo TheFileInfo;
        public string CustomProperty
        {
           get
           {
               if(this.TheFileInfo != null)
                    return this.TheFileInfo.FileName + this.TheFileInfo.FullName;
                return string.Empty;
           }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文