Dialog to Browse file and save it

Jul 21, 2015 at 5:06am
hey
I am new to c++. I am trying to create a editor program.I am stuck at how to open a dialog box where it asks to open a file from any directory (my pictures etc ) and then save that file.i tried using the form to create a open/save dialog box- in design mode - but when i compile it,its giving me errors like

button1_click has no member...if anyone who can guide me through this that would be amazing thankyou.

i am using visual studio 2013

here's that code
1
2
3

System::EventHandler(this, &Form::button1_Click);
Last edited on Jul 21, 2015 at 8:50am
Jul 21, 2015 at 2:14pm
First, I must say, you should start with console programs before windows programs.
Second, here's your answer:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913(v=vs.85).aspx
Jul 21, 2015 at 5:10pm
I disagree. Start with GUI programs.

Here's how to use the common "open file" dialog without having to play with COM interfaces (just straight-up Windows API):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

int main()
{
  char filename[ MAX_PATH ];

  OPENFILENAME ofn;
    ZeroMemory( &filename, sizeof( filename ) );
    ZeroMemory( &ofn,      sizeof( ofn ) );
    ofn.lStructSize  = sizeof( ofn );
    ofn.hwndOwner    = NULL;  // If you have a window to center over, put its HANDLE here
    ofn.lpstrFilter  = "Text Files\0*.txt\0Any File\0*.*\0";
    ofn.lpstrFile    = filename;
    ofn.nMaxFile     = MAX_PATH;
    ofn.lpstrTitle   = "Select a File, yo!";
    ofn.Flags        = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
  
  if (GetOpenFileNameA( &ofn ))
  {
    std::cout << "You chose the file \"" << filename << "\"\n";
  }
  else
  {
    // All this stuff below is to tell you exactly how you messed up above. 
    // Once you've got that fixed, you can often (not always!) reduce it to a 'user cancelled' assumption.
    switch (CommDlgExtendedError())
    {
      case CDERR_DIALOGFAILURE   : std::cout << "CDERR_DIALOGFAILURE\n";   break;
      case CDERR_FINDRESFAILURE  : std::cout << "CDERR_FINDRESFAILURE\n";  break;
      case CDERR_INITIALIZATION  : std::cout << "CDERR_INITIALIZATION\n";  break;
      case CDERR_LOADRESFAILURE  : std::cout << "CDERR_LOADRESFAILURE\n";  break;
      case CDERR_LOADSTRFAILURE  : std::cout << "CDERR_LOADSTRFAILURE\n";  break;
      case CDERR_LOCKRESFAILURE  : std::cout << "CDERR_LOCKRESFAILURE\n";  break;
      case CDERR_MEMALLOCFAILURE : std::cout << "CDERR_MEMALLOCFAILURE\n"; break;
      case CDERR_MEMLOCKFAILURE  : std::cout << "CDERR_MEMLOCKFAILURE\n";  break;
      case CDERR_NOHINSTANCE     : std::cout << "CDERR_NOHINSTANCE\n";     break;
      case CDERR_NOHOOK          : std::cout << "CDERR_NOHOOK\n";          break;
      case CDERR_NOTEMPLATE      : std::cout << "CDERR_NOTEMPLATE\n";      break;
      case CDERR_STRUCTSIZE      : std::cout << "CDERR_STRUCTSIZE\n";      break;
      case FNERR_BUFFERTOOSMALL  : std::cout << "FNERR_BUFFERTOOSMALL\n";  break;
      case FNERR_INVALIDFILENAME : std::cout << "FNERR_INVALIDFILENAME\n"; break;
      case FNERR_SUBCLASSFAILURE : std::cout << "FNERR_SUBCLASSFAILURE\n"; break;
      default                    : std::cout << "You cancelled.\n";
    }
  }
}

Here's the documentation for the function itself:
http://www.google.com/search?btnI=1&q=msdn+GetOpenFileName+function

There are all kinds of options you can play with:
http://www.google.com/search?btnI=1&q=msdn+OPENFILENAME+structure

Make sure to read the docs carefully -- some things require some special care, such as OFN_ALLOWMULTISELECT.

Also, notice that my example uses char as the character type, but it is structured such that you can easily change it to use wchar_t. (Change the element type of filename and change the 'A' on the end of the function to a 'W' and fix the string literals to be L"wide literals".)


Oh, almost forgot -- you need to link with comdlg32.lib. (You can read what to include and what library to link with at the bottom of the GetOpenFileName() page.)

Hope this helps.
Jul 21, 2015 at 6:33pm
@OP:

You seem (like many others) that you are not aware that what you are using now is NOT C++ programming language as you may think.
Jul 21, 2015 at 6:58pm
@modoran
It's "Managed C++", which looks and behaves as much like standard C++ as you like.

A valid C++ answer is also a valid answer for MS's managed stuff/CLI (usually).
Jul 27, 2015 at 3:27pm
Given the Managed C++ event handler I assume you created a Windows Forms Application? If that is the case, rather than using the old WinAPI GetOpenFileName() function or the new school COM-based IFileDialog/etc approach you should prob use the .NET Forms equivalent.

Basically, if you're writing a Forms app use the Forms approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 2;
      openFileDialog1->RestoreDirectory = true;

      if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
         // do your stuff
      }
   }


Example code lifted from here:

https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog%28v=vs.110%29.aspx

Andy
Last edited on Jul 27, 2015 at 3:32pm
Topic archived. No new replies allowed.