Hi friends! In this article, we’ll be discussing about how you can easily set filters for OpenFileDialog and SaveFileDialog in C# and see some simple examples.
What are Filters?
Filters are used in OpenFileDialog and SaveFileDialog in C# to specify the type of files that can be opened or saved.
A filter is a string that indicates which file types should be displayed in the dialog box and contains one or more file types, separated by the pipe symbol “|”.
For instance, you can set the filter to “.txt|.csv” if you only want the user to be able to access or save.txt and.csv files.
How to Set a Filter for OpenFileDialog?
To set a filter for OpenFileDialog, you can use the Filter property.
Here is a simple example:
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";
In this example, the filter allows the user to choose to open either a text file or a CSV file.
How to Set a Filter for SaveFileDialog?
Similarly, to set a filter for SaveFileDialog, you can use the Filter property.
Here’s a simple example:
SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";
In this example, the filter allows the user to save either a text file or a CSV file.
Let’s see a Full Example on Using Filters
Based on all the above, let’s see a full example on how we can easily use file filters in the OpenFileDialog and SaveFileDialogs in C#:
using System; using System.Windows.Forms; namespace FileFiltersExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Event handling code for when opening a file private void btnOpenFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv"; if (openFileDialog.ShowDialog() == DialogResult.OK) { MessageBox.Show("You selected file: " + openFileDialog.FileName); // add file handling code here... } } // Event handling code for when saving a file private void btnSaveFile_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { MessageBox.Show("You selected file: " + saveFileDialog.FileName); // add file handling code here... } } } }
Get the help you need to get started with .NET Programming fast and easy!
Enroll to our online course titled “.NET Programming for Beginners – Windows Forms with C#”
(special limited-time discount included in link).Learn how to implement Windows Forms projects in .NET using Visual Studio and C#, how to implement multithreading, how to create deployment packages and installers for your .NET Windows Forms apps using ClickOnce in Visual Studio, and more!
Many live demonstrations and downloadable resources included!
Recommended Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Data Management for Beginners – Main Principles
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Read Also:
- How to Connect to SQL Server from Visual C++
- What is Abstraction in Object Oriented Programming?
- How to Establish a Simple Connection from a C# Program to SQL Server
- The timeout period elapsed prior to obtaining a connection from the pool
- Closing a C# Application (including hidden forms)
- Changing the startup form in a C# project
- Using the C# SqlParameter Object for Writing More Secure Code
- Cannot implicitly convert type ‘string’ to ‘System.Windows.Forms.DataGridViewTextBoxColumn
- Using the C# SqlParameter Object for Writing More Secure Code
- How to Build a Simple Image Viewer with .NET WinForms and C# in Visual Studio
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.