allow user to select directory path


Well-known member
Nov 13, 2012
Programming Experience
In a new C# 2010 windows form application that I will be writing, I have the following items to mention:

1. The form will have the following 2 places for the user to select/enter data:
a. customer id, and b. a window where the user can select the directory path of
where they want to select a file from.
2. There will also be a submit button to click to show all the data has been entered, and
3. an area that will show when an invalid customer id has been entered.
4. The following is a url that I know that I can use as a reference for allowin the user to select
the directory of the file they want to use: OpenFileDialog Class (System.Windows.Forms).

Now I have the following questions to ask:
1. How would I setup a default directory for where the file should be located?
2. How should I setup code that will allow the user to select the directory path of where a file is located at? Do you need to have an extra button for the user to be able to 'select' the directory path?
3. How Do I take the values that have been entered on the windows form and pass the values back to the main method that called the windows form?
4. Can you show me code how on to tell the user they entered an invalid customer id?
First of all, an OpenFileDialog is intended to allow the user to select a source file. It can be the source for whatever you like (read, copy, etc) but it is for selecting a file, as the name suggests. If you want the user to select a folder rather than a file then you use a FolderBrowserDialog.

1. Open the Settings page of the project properties and add a String setting there. You can then use it in code via Properties.Settings.Default. For instance, if you named the setting FolderPath then you can get its value from Properties.Settings.Default.FolderPath. You can set that same property to save a new value. You could probably leave the setting empty by default and, when the app runs, test the value and, if it's empty, prompt the user for a path.

2. That's up to you. Do you want the user to click a Button to select the folder? If so then you add a Button. If you want the user to initiate the action some other way then you build it accordingly. You don't need any programing experience whatsoever to decide how you want your app to work from a user's perspective. We've all used software before many times and we know what works and what doesn't. We can all design an interface without any code at all.

3. Via properties. Add one or more properties to the dialogue form and expose the data through them. In the calling form, if ShowDialog returns OK then you get the data from those properties.

4. Um, set the Text of a Label maybe? Do we really need to explain how to display some text in a UI? That seems rather elementary.

Latest posts

Top Bottom