0

hiya, i am new in C++ and need to create a windows form application that at the click of the button the class is called and excecuted. I have done GUI's in java and its no big issue for me, its just that I need the GUI in c++ since the class files are in c++, I have tried looking at JNI's but it can get too complicated.

Any help would be greatly appreciated

3
Contributors
17
Replies
18
Views
6 Years
Discussion Span
Last Post by ragoo
0

This is the code

#pragma once

namespace gui {

    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 Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            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(51, 202);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        this->button1->UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(292, 273);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

            }




#pragma endregion
    };
}

Edited by mike_2000_17: Fixed formatting

0

sorry

#pragma once

namespace gui {

	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 Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			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(51, 202);
		this->button1->Name = L"button1";
		this->button1->Size = System::Drawing::Size(75, 23);
		this->button1->TabIndex = 0;
		this->button1->Text = L"button1";
		this->button1->UseVisualStyleBackColor = true;
		// 
		// Form1
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(292, 273);
		this->Controls->Add(this->button1);
		this->Name = L"Form1";
		this->Text = L"Form1";
		this->ResumeLayout(false);

			}

	
		

#pragma endregion
	};
}
0

In Forms designer, double click on the button and the IDE will generate an OnClick event handler. Then just add the code shown below.

#include "Form2.h"
<snip>
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 Form2^ form2 = gcnew Form2;
                 form2->Show();
             }

Edited by Ancient Dragon: n/a

0

thanks you for the reply. Forgive me for asking this question, what is contained in the form2.h, since the compiler will ask me where is form2.h defined

Thanks

0

You stated you wanted to open another form when you pressed a button. Form2.h contains that form. Each form is in another class. Change Form2 to whatever you named the form you want to display when the button is clicked.

0

Thanks for that. I get an error when I compile.

Error	1	error C2065: 'Form2' : undeclared identifier	
Error	2	error C2065: 'form2' : undeclared identifier	
Error	3	error C2061: syntax error : identifier 'form2'	
Error	4	error C2065: 'Form2' : undeclared identifier	
Error	5	error C2227: left of '->Show' must point to class/struct/union/generic type

Edited by ragoo: wrong computer

0

This is the code

#include "Form2.h"

#pragma once

namespace gui {

	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 Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			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(39, 170);
		this->button1->Name = L"button1";
		this->button1->Size = System::Drawing::Size(75, 23);
		this->button1->TabIndex = 0;
		this->button1->Text = L"button1";
		this->button1->UseVisualStyleBackColor = true;
		this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
		// 
		// Form1
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(292, 273);
		this->Controls->Add(this->button1);
		this->Name = L"Form1";
		this->Text = L"Form1";
		this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
		this->ResumeLayout(false);

			}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Form2^ form2 = gcnew form2;               
				 Form2->Show();
				 }
		};
}
0

>> Form2^ form2 = gcnew form2;

Watch the capitalization. Should be Form2^ form2 = gcnew Form2; Move line 1 down below that #pragma once to let the compiler's preprocessor be more efficient.

Edited by Ancient Dragon: n/a

0

Thanks for that. I get an error when I compile.

He just said 'Form2' as an example name for the new form you want to open.

Goto Project->Add Class->Windows Form. In there, call it what you wish and the code given should open the new form for you. :)

0

Thanks for that but it still shows

Error	1	error C2065: 'Form2' : undeclared identifier	86
Error	2	error C2065: 'form2' : undeclared identifier		86
Error	3	error C2061: syntax error : identifier 'Form2'	86
Error	4	error C2065: 'Form2' : undeclared identifier	87
Error	5	error C2227: left of '->Show' must point to class/struct/union/generic type	87
0

Repost the current program.

#pragma once

#include "HelloWorld.h"


namespace Trial {

	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 Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			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(44, 195);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 273);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	
	private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

         App app; // Create a instance of App

         HelloWorld^ form2 = gcnew HelloWorld;           
		 
		 form2->Show();
       }

	};
}
0

Apologises for the late reply

This is in the HelloWorld.h

#include <iostream> 

using namespace std; // use std namespace for cout 

ref class App // change name to app
{
public:
 App() // say hello when object is constructed
 {
  Say("IO!");
 };
 ~App() // say goodbye when object is destroyed
 {
  Say("File!");
 };
 void Say(char* Message)
 {
  cout << Message << endl;
 };

 char* Show(char* Message)
 {
   return Message;
 }
};
0

These are the Errors

Error	1	error C2065: 'HelloWorld' : undeclared identifier		86
Error	2	error C2065: 'form2' : undeclared identifier		86
Error	3	error C2061: syntax error : identifier 'Form2'		86
Error	4	error C2065: 'form2' : undeclared identifier		88
Error	5	error C2227: left of '->Show' must point to  class/struct/union/generic type		88
0

New error

Error	1	error C2065: 'HelloWorld' : undeclared identifier		93
Error	2	error C2065: 'form2' : undeclared identifier		93
Error	3	error C2061: syntax error : identifier 'HelloWorld'		93
Error	4	error C2065: 'form2' : undeclared identifier		94
Error	5	error C2227: left of '->Show' must point to class/struct/union/generic type
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.