6.7. Exploring the File Dialog

Nobody really wants to type in a file name, let alone a file name and path. What users want is a directory tree dialog window to pop up that they can navigate and select from. The first step, as always, is to drag the control onto the form. Unlike the other controls so far, OpenFileDialog is not a visible control. It does not display in the form. Rather we need to associate it with a visible control. The usual suspect is an Open menu item.

When the user clicks the menu item, we need to respond by invoking the openFileDialog1 object's ShowDialog() member function. The menu item event handler looks like this:

 private void menuItem2_Click(object sender, System.EventArgs e) { Text = "Open Menu Item Clicked"; openFileDialog1.ShowDialog(); ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.