FileDialog.Filter 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置筛选器字符串,该字符串确定从OpenFileDialogSaveFileDialog或 ..
public:
property System::String ^ Filter { System::String ^ get(); void set(System::String ^ value); };
public string Filter { get; set; }
member this.Filter : string with get, set
Public Property Filter As String
属性值
包含筛选器的 A String 。 默认值 Empty为,这意味着不应用任何筛选器,并显示所有文件类型。
例外
筛选器字符串无效。
示例
以下示例演示了可以使用该 Filter 属性设置的多种筛选器字符串。
OpenFileDialog dlg = new OpenFileDialog();
// Show all files
dlg.Filter = string.Empty;
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Show all files
dlg.Filter = String.Empty
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Show all files
dlg.Filter = null;
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Show all files
dlg.Filter = Nothing
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by Word Documents
dlg.Filter = "Word Documents|*.doc";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by Word Documents
dlg.Filter = "Word Documents|*.doc"
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by Excel Worksheets
dlg.Filter = "Excel Worksheets|*.xls";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by Excel Worksheets
dlg.Filter = "Excel Worksheets|*.xls"
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by PowerPoint Presentations
dlg.Filter = "PowerPoint Presentations|*.ppt";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by PowerPoint Presentations
dlg.Filter = "PowerPoint Presentations|*.ppt"
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by Office Files
dlg.Filter = "Office Files|*.doc;*.xls;*.ppt";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by Office Files
dlg.Filter = "Office Files|*.doc;*.xls;*.ppt"
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by All Files
dlg.Filter = "All Files|*.*";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by All Files
dlg.Filter = "All Files|*.*"
dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();
// Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations
// OR Office Files
// OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" +
"|Office Files|*.doc;*.xls;*.ppt" +
"|All Files|*.*";
dlg.ShowDialog();
Dim dlg As New OpenFileDialog()
' Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations
' OR Office Files
' OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" & "|Office Files|*.doc;*.xls;*.ppt" & "|All Files|*.*"
dlg.ShowDialog()
注解
如果 Filter 显示 null 或 Empty显示所有文件,并且始终显示文件夹。
可以通过设置 Filter 属性指定要显示的文件类型的子集。 每种文件类型都可以表示特定类型的文件,如下所示:
Word 文档(*.doc)
Excel 工作表(*.xls)
PowerPoint 演示文稿(*.ppt)
或者,文件类型可以表示一组相关的文件类型,例如:
Office 文件(*.doc、*.xls、*.ppt)
所有文件 (*.*)
若要指定要显示的文件类型的子集,请使用字符串值(筛选器字符串)设置Filter属性,该属性指定要筛选的一个或多个文件类型。 下面显示了筛选器字符串的预期格式:
FileType1[[|FileType2]...[|FileTypeN]]
使用以下格式描述每种文件类型:
Label|Extension1[[;Extension2]...[;ExtensionN]]
Label 部件是一个可读字符串值,用于描述文件类型,如下所示:
“Word 文档”
“Excel 工作表”
“PowerPoint 演示文稿”
“Office 文件”
“所有文件”
每个文件类型必须至少由一个 扩展名描述。 如果使用多个 扩展 ,则每个 扩展 必须用分号(“;”)分隔。 例如:
“*.doc”
“*.xls;”
“*.ppt”
“*.doc;*.xls;*.ppt”
"*.*"
下面是有效 Filter 字符串值的完整示例:
Word Documents|*.docExcel Worksheets|*.xlsPowerPoint Presentations|*.pptOffice Files|*.doc;*.xls;*.pptAll Files|*.*Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt|Office Files|*.doc;*.xls;*.ppt|All Files|*.*
筛选器中包含的每个文件类型都作为单独的项添加到类型为:或SaveFileDialog 的下拉列表中的OpenFileDialog下拉列表,如下图所示。
,
用户可以从此列表中选择要筛选依据的文件类型。 默认情况下,列表中的第一项(例如,第一个文件类型)在显示或显示时OpenFileDialogSaveFileDialog处于选中状态。 若要指定要选择的另一个文件类型,请在显示OpenFileDialog或SaveFileDialog(通过调用ShowDialog)之前设置FilterIndex属性。