OpenFileDialog in C#.Net windows application

Let us discuss on OpenFileDialog in C#.Net windows application. Also, we will see how to open windows form as a dialog box in C#.Net.

OpenFileDialog in C#.Net windows application

OpenFileDialog class provides a dialog box that enables the user to select one or more files.

OpenFileDialog allows users to browse folders and select files. It is available in Windows Forms and can be used with C# code. It displays the standard Windows dialog box. The results of the selection can be read in your C# code.

In this example we will see whenever a user will select an image through the OpenFileDialog, the image will appear in the picture box.

So First Drag and drop a picture box and a button to the form. Then in the button click event write the below code.

private void button1_Click(object sender, EventArgs e)
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = “Open Photo”;
dlg.Filter = “jpg files (*.jpg)|*.jpg|All files (*.*)|*.*”;
if (dlg.ShowDialog() == DialogResult.OK)
pictureBox1.Image = new Bitmap(dlg.OpenFile());

Once user selects an image, the image will appear.

Open windows form as modal dialog box in C#.Net

Now, we will see how to open windows form as a dialog box in C#.Net.

We can open a windows form as a dialog box by using the ShowDialog method of the windows form. Below is the code:

MaximizeTest objMaximizeTest = new MaximizeTest();

Here MaximizeTest is the windows form name and we are calling the ShowDialog() Method of the class.

But if you want to return the DialogResult then you can write like below:

MaximizeTest objMaximizeTest = new MaximizeTest();
DialogResult result = objMaximizeTest.ShowDialog();

DialogResult is an enum which can contain a value like: Abort, Cancel, Ignore, No, None, OK, Retry, Yes.

According to these result, you can perform some action.

You may like following C#.Net tutorials:

I hope this C#.Net tutorial explains OpenFileDialog in C#.Net windows application and how to open windows form as a dialog box in C#.Net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site