InfinityCounter
InfinityCounter

Reputation: 384

Save and Open file Dialogs

I AM USING C++. I am currently designing a program with windows forms. I am having a problem writing event handlers. especially the click event handler for save and open file. I have searched the net and can find no decent explanation on how to write these event handlers. So i am asking for a well defined definition.

Please do not send me to Microsoft as they do not provide a sample of completed event handlers only partially.

Upvotes: 1

Views: 13402

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

Since it's not obvious at a glance that Visual Studio 2012 does support C++/CLI Winform applications, even though the OP certainly knows this, for other readers, here's how I created one just to answer this question:

  1. In Visual Studio 2012, in the New Project dialog I selected [Visual C++ > CLR > CLR Empty Project].

  2. In the new project I added an ordinary C++ main function and a Windows Form.

  3. In the linker settings I changed it from console subsystem to GUI subsystem (and due to the non-standard behavior of Microsoft's linker, adjusted the entry point to mainCRTStartup).

That said, below is shown the generated Winform header, with just the event handler code, which fires up an Open File dialog, added manually.

  • The long comment line ------ shows how to connect up an event handler.

  • The code at the bottom shows both how to code up an event handler, and how to run the open file dialog.

#pragma once

namespace CppWindowsFormsApplication {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MainWindow
    /// </summary>
    public ref class MainWindow : public System::Windows::Forms::Form
    {
    public:
        MainWindow(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MainWindow()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(149, 208);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"&Open file";
            this->button1->UseVisualStyleBackColor = true;

            // ---------------------------------------------------- !Adds event handler -------------------
            this->button1->Click += gcnew System::EventHandler(this, &MainWindow::button1_Click);
            // 
            // MainWindow
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->button1);
            this->Name = L"MainWindow";
            this->Text = L"MainWindow";
            this->ResumeLayout(false);

        }
#pragma endregion
    private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
        {
            OpenFileDialog^ sfd = gcnew OpenFileDialog();
            sfd->Filter = "Text Files|*.txt|All Files|*.*";
            if( sfd->ShowDialog() != System::Windows::Forms::DialogResult::OK )
            {
                return;
            }
            //MessageBox::Show( sfd->FileName );
            MessageBox::Show( "OK" );
        }
    };
}

I didn't fix the indenting etc., since I didn't have the time; nearly all of this code was generated by Visual Studio 2012's RAD (Rabid Application Development) feature.


Related to this question’s history, now removed:

I didn't really have the time to answer this question. I would much rather have let someone else answer it (and reap some reputation from it). It is such a simple question that I’m sure if it wasn’t closed so fast, any number of readers, with more time than I had, would have answered it.

But answering it, with code posted elsewhere, and forsaking some other matters, was the only way I saw to have it reopened.

I sincerely ask every reader to please don't vote to close what you don’t understand.

Only vote to close a question when you thoroughly understand the subject matter and can prove to yourself and others that the community is better off with the question closed.

Failure to understand a question does not necessarily mean that the question is ungood.

Upvotes: 5

Related Questions