|
|
|
|
File open, save dialogsLets see how to use file open save dialogs. Here is the interface you have to build.
What we are going to do is when the user clicks browse, display a file open dialog and then take the path of the file selected by user and enter in to the text field. Add a CString variable with name "path" for the text field. Category should be "value". Here is the function which handles clicking the browse button. void CfileopenDlg::OnBnClickedButton1() CFileDialog is the class for the file open and save dialogs. In the first line in the function, we create a variable "dlg" of the type CFileDialog, and we pass TRUE to the constructor. That is for specify that this dialog is a file open dialog. If you send FALSE, the dialog will be a file save dialog. Then, in the second line, we call DoModal(), which shows the dialog. The status of the user's respond is returned to the variable "result", when either user selects a file by the dialog, he cancels it or he closes it. If he selects a file, the returned value is IDOK. So, we check whether dialog returned IDOK, and if so, we put the path name the user selected to the variable "path", which we assigned to the text field. Then we call UpdateData() with argument FALSE, because this time, we need to send the content of the variable "path" to the text field, not to take the content of the text field to the variable. Compile and run the application and see the result. Download the source code. |