Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 11, 2013 10:17 PM
In a new C# 2010 windows form application that I will be writing, I have the following items to mention:
- 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: http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx.
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?
All replies (6)
Tuesday, June 11, 2013 11:55 PM ✅Answered | 1 vote
The OpenFileDialog is used to help a user select a file. Your requirement seems to be that you want the user to select a folder. To select a folder you should use the FolderBrowserDialog http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx
There is an example of how to use it on the MSDN page.
Paul Linton
Wednesday, June 12, 2013 11:01 PM ✅Answered
If you are using WPF, there is not easy way to get to the file/folder explorer. I have code that gives an example but it will require you to add the System.Windows.Form dll in order to use the FolderBrowserDialog. This code can be modified to work with the OpenFileDialog as well.
You should follow Paul's advice and experiment with the code, that is after all how we all learned. The documentation he linked earlier is very helpful.
// This line calls the folder diag
System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
// This is what will execute if the user selects a folder and hits OK (File if you change to FileBrowserDialog)
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string folder = dlg.SelectedPath;
}
else
{
// This prevents a crash when you close out of the window with nothing
}
Wednesday, June 12, 2013 3:48 AM
I want to be able to set a default folder location but let the user select the files they need. How would you accomplish this goal?
Wednesday, June 12, 2013 3:50 AM
If they are selecting a file then you should use the OpenFileDialog. If you read the documentation that your original post points to you will see how to set the Initial Directory that the OpenFileDialog takes the user to.
Paul Linton
Wednesday, June 12, 2013 9:45 PM
If I want to use WPF (windows presentation foundation) to set up the desktop application, would selecting the file location use the same logic you recommended or differet logic? If the logic is different, can you tell me how I would select a file location in WPF?
Wednesday, June 12, 2013 10:17 PM
Try it, experiment, learn.
Paul Linton