我的任务是显示给定目录中的文件列表。以下是我现有的代码:
FileListBox1->FileEdit = Edit1;
FilterComboBox1->FileList = FileListBox1;
DirectoryListBox1->FileList = FileListBox1;
DirectoryListBox1->DirLabel = Label1;
DriveComboBox1->DirList = DirectoryListBox1;
Button1->Default = true;
到目前为止,一切正常,但目录中只有 *.txt
文件,我想跳过这种类型的提要并立即将文件扩展名缩小到 *.txt
。
我已经尝试过这样的事情:
String fileType = L"*.txt";
FileListBox1->FileEdit = fileType;
还有一些其他的解决方案,但是每次都报错。
有什么想法吗?
最佳答案
您收到一个错误,因为您正试图在需要 TEdit*
的地方分配一个 String
。
如果您希望 TFileListBox
以可编程方式显示特定类型的文件,请去掉 TFilterComboBox
并使用 TFileListBox::Mask
属性代替:
Limits which files are displayed in the file list box.
Set Mask to a regular expression to limit the list box to files that match the mask. The value of the mask is a file name that may include wildcards.
String fileType = _D("*.txt");
FileListBox1->Mask = fileType;
也就是说,您正在使用的文件 UI 组件古老,它们是在 Windows 3.1 时代设计的,不应在现代软件中使用。
查看 RTL 的 FindFirst()
和 FindNext()
函数以可编程方式枚举带有掩码过滤的文件。参见 Finding a File在 Embarcadero 的文档中了解更多详细信息。
关于c++ - 如何显示目录中的文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050296/