Opening a file in windows forms
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Opening a file in windows forms

  1. #1
    Join Date
    Mar 2011
    Posts
    25

    Opening a file in windows forms

    Hey,

    I am having a bit of trouble with finding out how to open up a file directory at the click of a button in VC++ Windows Forms.

    I know there is the openfiledialog that would open up given the right instructions but I cannot have a menu bar at the top, so I need to be able to do this by a button click.

    If anyone could help that would be great, thanks!

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Opening a file in windows forms

    You mean you want to open the OpenFileDialog in response to a button click instead of the invocation of a menu item? That's no problem, the dialog is in no way predetermined to only be used in conjunction with menus. Just set up your open file dialog object and call its ShowDialog() method, that's about it. BTW, the sample code on the MSDN page on the OpenFileDialog class does exactly that.

    If you want to open the file without using the open file dialog, the first question to answer is: How then do you want to acquire the file path instead?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Mar 2011
    Posts
    25

    Re: Opening a file in windows forms

    I have tried what is on the MSDN page, maybe I am not declaring something properly, as I am getting out a LOT of errors, most talking about identifiers not being declared or the like. Tried looking around the net aswell to see if there were other example codes I could look at and tweak but so far nothing has came up.

    What I had done was try and put the code in after the button click event handler, although now I am presuming that is wrong, but I do not know where to declare and what to declare. On the Msdn example it says to use syntax but whenever I put that in at the begining it overwrites what is there originally and thus causes errors in parts of the program where it shouldn.t.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Opening a file in windows forms

    Ok, the sample doesn't mention that it needs an additional

    Code:
    using namespace System::IO;
    for the Stream class to be available. Aside from that it shouldn't be problematic.

    I have attached a sample project (a simple text file viewer) that contains a button click handler which does the majority of the program's work and is tightly based on the MSDN sample:

    Code:
    void Form1::button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
    {
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
    
      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 1;
      openFileDialog1->RestoreDirectory = true;
    
      if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
        if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
        {
          // Insert code to read the stream here.
    
          // Add file name without path to the form title
    
          Text = String::Concat("Simple Text File Viewer - ", openFileDialog1->SafeFileName);
    
          // Write complete file path to file path text box
    
          txtFilePath->Text = openFileDialog1->FileName;
    
          // Load text file contents into viewer text box:
    
          txtText->Text = (gcnew StreamReader(myStream))->ReadToEnd();
    
          myStream->Close();
        }
      }
    }
    Note, however, that I implemented the event handlers in a Form1.cpp file rather than the .h file as the IDE sets it up. This is generally considered better style (at least in native C++) and has advantages when building the project (though some of them only really pay for larger projects).
    Attached Files Attached Files
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center