In globaldata.h I am defining a typedef struct as GlobalData. In my main cpp program, I'm including globaldata.h, then declaring
GlobalData globalData;
After that declaration, I include createNetDialog.h and gui.h. Why can I only reference globalData in gui.h but not createNetDialog.h? Both include files should be pasted into my .cpp file during precompile, so globalData should be visible in both right?
I have a GlobalData structure defined here:
globaldata.h
typedef struct {
string netFilename;
DataFileReader *dataFileReader;
} GlobalData;
In my c++ code, I have:
gui.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "DataFileReader.h"
#include "globaldata.h"
GlobalData globalData;
#include "createNetDialog.h"
#include "gui.h"
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
[STAThread]
void Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Carnac::gui formMain;
Application::Run(%formMain);
}
createNetDialog.h (in the createNetDialog constructor)
#pragma once
namespace Carnac {
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 createNetDialog
/// </summary>
public ref class createNetDialog : public System::Windows::Forms::Form
{
public:
createNetDialog(void)
{
InitializeComponent();
// left of '.size' must have a class/struct/union
// left of '.dataFileReader' must have a class/struct/union
// 'globalData':undeclared identifier despite being declared right before
// this file is included in the .cpp file
for (unsigned int i=0; i<globalData.dataFileReader->_headerItems.size(); i++)
{
// do something
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~createNetDialog()
{
if (components)
{
delete components;
}
}
protected:
protected:
private: System::Windows::Forms::Button^ buttonCreate;
private: System::Windows::Forms::Button^ buttonCreateCancel;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->buttonCreate = (gcnew System::Windows::Forms::Button());
this->buttonCreateCancel = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// buttonCreate
//
this->buttonCreate->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->buttonCreate->Location = System::Drawing::Point(16, 288);
this->buttonCreate->Name = L"buttonCreate";
this->buttonCreate->Size = System::Drawing::Size(75, 23);
this->buttonCreate->TabIndex = 7;
this->buttonCreate->Text = L"Create";
this->buttonCreate->UseVisualStyleBackColor = true;
//
// buttonCreateCancel
//
this->buttonCreateCancel->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
this->buttonCreateCancel->Location = System::Drawing::Point(404, 288);
this->buttonCreateCancel->Name = L"buttonCreateCancel";
this->buttonCreateCancel->Size = System::Drawing::Size(75, 23);
this->buttonCreateCancel->TabIndex = 8;
this->buttonCreateCancel->Text = L"Cancel";
this->buttonCreateCancel->UseVisualStyleBackColor = true;
this->buttonCreateCancel->Click += gcnew System::EventHandler(this, &createNetDialog::buttonCreateCancel_Click);
//
// createNetDialog
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(491, 323);
this->Controls->Add(this->buttonCreateCancel);
this->Controls->Add(this->buttonCreate);
this->Name = L"createDialog";
this->Text = L"CreateDialog";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void buttonCreateCancel_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
}
};
}
gui.h (at the end)
#pragma once
#include <msclr\marshal_cppstd.h>
namespace Carnac {
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 gui
/// </summary>
public ref class gui : public System::Windows::Forms::Form
{
public:
gui(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~gui()
{
if (components)
{
delete components;
}
}
protected:
private: int _dataColumnSelected;
private: System::Windows::Forms::Button^ buttonLoad;
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->buttonLoad = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// buttonLoad
//
this->buttonLoad->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
this->buttonLoad->Text = L"Load Data";
this->buttonLoad->UseVisualStyleBackColor = true;
this->buttonLoad->Click += gcnew System::EventHandler(this, &gui::buttonLoad_Click);
this->buttonLoad->Location = System::Drawing::Point(67, 66);
this->buttonLoad->Name = L"buttonLoad";
this->buttonLoad->Size = System::Drawing::Size(75, 23);
this->buttonLoad->TabIndex = 0;
this->buttonLoad->UseVisualStyleBackColor = true;
//
// gui
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::AppWorkspace;
this->ClientSize = System::Drawing::Size(234, 171);
this->Controls->Add(this->buttonLoad);
this->Name = L"gui";
this->Text = L"Carnac";
this->ResumeLayout(false);
}
#pragma endregion
static std::string toStandardString(System::String^ string)
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
std::string returnString(charPointer, string->Length);
Marshal::FreeHGlobal(pointer);
return returnString;
}
private: System::Void buttonPredictRun_Click(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void buttonTestLoad_Click(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void buttonLoad_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->Filter = "Data Files|*.dat";
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
ifstream inFile;
String^ strfilename = openFileDialog1->InitialDirectory + openFileDialog1->FileName;
// THIS WORKS
// Why can I use globalData here but not in createNetDialog.h?
globalData.dataFileReader = new DataFileReader(toStandardString(strfilename));
}
}
};
}
Thanks for any help
Yes, globalData is visible in both gui.h and createNetDialog.h, when compiled from gui.cpp. In globalData.h all you have defined is a type, not a global variable of that type. When compiled from any other cpp file, gui.h and createNetDialog.h can't see the declaration of the global variable in gui.cpp.
Here's what you want to do: declare both the type, and an extern global variable in globalData.h. Include globalData.h from every file you want to use it in, don't rely on other files to have included it before the one you want to use it in. In just one cpp file (currently gui.cpp), declare the actual global variable.
// globalData.h
#include "DataFileReader.h"
typedef struct {
string netFilename;
DataFileReader *dataFileReader;
} GlobalData;
extern GlobalData globalData;
.
// EVERY file where you use globalData
#include "globalData.h"
.
// gui.cpp
#include "globaldata.h"
GlobalData globalData;
Related
How can I output some value from MyForm.cpp file to MyForm.h richtextbox?
In my MyForm.h
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBox^ richTextBox1;
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->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->SuspendLayout();
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(13, 13);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(259, 237);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->richTextBox1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->ResumeLayout(false);
}
#pragma endregion
public: void insertText(std::string msg) {
this->richTextBox1->Text = gcnew String(msg.c_str());
this->richTextBox1->SelectionColor = Color::Red;
}
};
In my MyForm.cpp
void Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Test1::MyForm form;
Application::Run(%form);
form.insertText("123");
}
The get no error during compile but it output nothing to the richtextbox. Is there any method to do that? Some of my code must be run in cpp file.
i want to pass the Form1 to Form2 via constructor but it is not working, i guess it is somenthing stupid from my side but i can't figure out what is wrong. I would appreciate some help here.
Here is Form1 containig a richtextbox and a button, which opens the second Form.
#pragma once
#include "Form2.h"
namespace passForm {
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public: System::Windows::Forms::RichTextBox^ richTextBox1;
public: System::Windows::Forms::Button^ open_form2_button;
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
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->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->open_form2_button = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(12, 12);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(100, 96);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
//
// open_form2_button
//
this->open_form2_button->Location = System::Drawing::Point(159, 47);
this->open_form2_button->Name = L"open_form2_button";
this->open_form2_button->Size = System::Drawing::Size(89, 23);
this->open_form2_button->TabIndex = 1;
this->open_form2_button->Text = L"Open Form 2";
this->open_form2_button->UseVisualStyleBackColor = true;
this->open_form2_button->Click += gcnew System::EventHandler(this, &Form1::open_form2_button_Click);
//
// Form1
//
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->open_form2_button);
this->Controls->Add(this->richTextBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void open_form2_button_Click(System::Object^ sender, System::EventArgs^ e) {
Form^ rgForm = gcnew Form2(this);
rgForm->Show();
//this->Hide();
}
};
}
And here is the Form2 with the problematic constructor
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace passForm {
/// <summary>
/// Summary for Form2
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
private: System::Windows::Forms::Form ^ otherform;
public:
Form2()
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
public:
Form2(System::Windows::Forms::Form ^ frm1)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
otherform = frm1; // this assignment is not working
}
private: System::Windows::Forms::Button^ btnShowResults;
public:
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
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->btnShowResults = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// btnShowResults
//
this->btnShowResults->Location = System::Drawing::Point(88, 108);
this->btnShowResults->Name = L"btnShowResults";
this->btnShowResults->Size = System::Drawing::Size(99, 23);
this->btnShowResults->TabIndex = 0;
this->btnShowResults->Text = L"Show Results";
this->btnShowResults->UseVisualStyleBackColor = true;
this->btnShowResults->Click += gcnew System::EventHandler(this, &Form2::btnShowResults_Click);
//
// Form2
//
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->btnShowResults);
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void btnShowResults_Click(System::Object^ sender, System::EventArgs^ e) {
otherform->richTextBox1->AppendText("Hello\n"); //richTextBox1 is not recognized
}
};
}
Please pay attention to following parts
Form1.h
private: System::Void open_form2_button_Click(System::Object^ sender, System::EventArgs^ e) {
Form^ rgForm = gcnew Form2(this);
rgForm->Show();
//this->Hide();
}
and Form2.h
private: System::Windows::Forms::Form ^ otherform;
public:
Form2()
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
public:
Form2(System::Windows::Forms::Form ^ frm1)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
otherform = frm1; // this assignment is not working
}
i guess those parts generate the errors.
Note that when i pass to the constructor only the RichTxtBox object everything is working fine.
For Example :
Form1.h :
#pragma once
#include "Form2.h"
namespace ArrayShallow {
ref class Form2;
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>
/// Description résumée de Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ajoutez ici le code du constructeur
//
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
this->richTextBox1->Location = System::Drawing::Point(32, 25);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(100, 96);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
this->button1->Location = System::Drawing::Point(153, 157);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Controls->Add(this->richTextBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private:
Form2 ^ currForm2;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e);
private: System::Void button1_Click(System::Object ^sender, System::EventArgs^ e);
public:
void SetValue(System::String ^text)
{
richTextBox1->AppendText(text);
}
};
}
Form1.cpp :
#include "stdafx.h"
#include "Form1.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace ArrayShallow;
System::Void Form1::Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
currForm2 = gcnew Form2(this);
}
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm2->Show();
}
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return (0);
}
And then in form2.h
#pragma once
#include "Form1.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace ArrayShallow {
ref class Form1;
/// <summary>
/// Description résumée de Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
private:
Form1 ^currForm1;
public:
Form2(Form1 ^theForm1)
{
InitializeComponent();
//
//TODO: ajoutez ici le code du constructeur
//
currForm1 = theForm1;
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(135, 107);
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, &Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
};
}
Event, in form2.cpp
#include "stdafx.h"
#include "Form2.h"
using namespace ArrayShallow;
System::Void Form2::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm1->SetValue("Hello\n");
}
When you'll execute this code :
Let me try to guess:
I'm sure the assignment is working, but then you do this:
otherform->richTextBox1->AppendText("Hello\n");
And that is not going to work, since System::Windows::Forms::Form does not have a member richTextBox1.
You probably want to define a handle to Form1, not Form.
private: Form1 ^ otherform;
On the other hand, I suggest that you use the events mechanism to communicate between forms. The second form fires an event passing the proper arguments, the first form installs a handle to that event and does the appropriate processing.
first, in you Form1 class, add a method who will set your RichTextBox value :
public:
void SetValue(System::String ^text)
{
richTextBox1->AppendText(text);
}
Then in your form2, on click button :
System::Void Form2::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm1->SetValue("Hello\n");
}
It has yet to do anything to make it compile, is to make an early statement in the Form1 Form2.h file. For example, in the first namespace
ref class Form1;
it will just do the same in the Form1.h file: declare form2 this way:
ref class Form2;
Your program works fine with me, if you have any questions, do not hesitate
I just type all the things like the example in my book. Could you tell me why it do not work?
Thank you for your attention.
This is the layout.
This is Example_1.cpp.
// Example_1.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Example_1;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
The following is the code.
#pragma once
namespace Example_1 {
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;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
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->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(154, 74);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(104, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"OK";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(118, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"Please enter you name.";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(15, 35);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(243, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(273, 110);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
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);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
String^ message = "Hello";
String^ title = "Welcome";
MessageBox::Show( message, title, MessageBoxButtons::OK );
}
};
}
The following is the output.
1>------ Build started: Project: Example_1, Configuration: Debug Win32 ------
1>Build started 02-Feb-14 6:28:11 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Example_1.unsuccessfulbuild".
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1> All outputs are up-to-date.
1> Example_1.cpp
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2653: 'MessageBoxButton' : is not a class or namespace name
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2065: 'OK' : undeclared identifier
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.88
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You forgot the namespace of the MessageBox class:
using namespace System::Windows;
maybe try this:
System::Windows::Forms::MessageBox
and this:
System::Windows::Forms::MessageBoxButtons
(you forgot the s)
link:
http://msdn.microsoft.com/en-us/library/system.windows.forms%28v=vs.110%29.aspx
and
http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons%28v=vs.110%29.aspx
So I am writing a Windows Form App, and so when I run it it throws an error that it should not be throwing. It throws a "Syntax Error :Missing ; Before using"
I tried putting one on the previous line and it did not work.
The File In Question is this
// Rock Paper Scisors.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace RockPaperScisors;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}}
stdafx.h is part of MS Visual C++ Express, anyway, The code for Form1.h can be found at this pastebin (didn't want to flood up the entire stack overflow page.) http://pastebin.com/324mpCps
All Help is Appreciated!
You just confused with the brackets.
Firstly, remove the last '}' in Rock Paper Scisors.cpp file. Secodnly, this is how the correct Form1.h file should look:
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctime>
#include<cstdlib>
#pragma once
namespace RockPaperScisors {
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^ Rock;
protected:
private: System::Windows::Forms::Button^ Paper;
private: System::Windows::Forms::Button^ cis;
private: System::Windows::Forms::TextBox^ ComBox;
private: System::Windows::Forms::TextBox^ WonBox;
private: System::Windows::Forms::Label^ CompLabel;
private: System::Windows::Forms::Label^ WonLabel;
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->Rock = (gcnew System::Windows::Forms::Button());
this->Paper = (gcnew System::Windows::Forms::Button());
this->cis = (gcnew System::Windows::Forms::Button());
this->ComBox = (gcnew System::Windows::Forms::TextBox());
this->WonBox = (gcnew System::Windows::Forms::TextBox());
this->CompLabel = (gcnew System::Windows::Forms::Label());
this->WonLabel = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// Rock
//
this->Rock->Location = System::Drawing::Point(13, 33);
this->Rock->Name = L"Rock";
this->Rock->Size = System::Drawing::Size(75, 23);
this->Rock->TabIndex = 0;
this->Rock->Text = L"Rock";
this->Rock->UseVisualStyleBackColor = true;
this->Rock->Click += gcnew System::EventHandler(this, &Form1::Rock_Click);
//
// Paper
//
this->Paper->Location = System::Drawing::Point(94, 33);
this->Paper->Name = L"Paper";
this->Paper->Size = System::Drawing::Size(75, 23);
this->Paper->TabIndex = 0;
this->Paper->Text = L"Paper";
this->Paper->UseVisualStyleBackColor = true;
this->Paper->Click += gcnew System::EventHandler(this, &Form1::Paper_Click);
//
// cis
//
this->cis->Location = System::Drawing::Point(175, 33);
this->cis->Name = L"cis";
this->cis->Size = System::Drawing::Size(75, 23);
this->cis->TabIndex = 0;
this->cis->Text = L"Scisors";
this->cis->UseVisualStyleBackColor = true;
this->cis->Click += gcnew System::EventHandler(this, &Form1::cis_Click);
//
// ComBox
//
this->ComBox->Location = System::Drawing::Point(83, 127);
this->ComBox->Name = L"ComBox";
this->ComBox->Size = System::Drawing::Size(100, 20);
this->ComBox->TabIndex = 1;
//
// WonBox
//
this->WonBox->Location = System::Drawing::Point(83, 190);
this->WonBox->Name = L"WonBox";
this->WonBox->Size = System::Drawing::Size(100, 20);
this->WonBox->TabIndex = 2;
//
// CompLabel
//
this->CompLabel->AutoSize = true;
this->CompLabel->Location = System::Drawing::Point(91, 95);
this->CompLabel->Name = L"CompLabel";
this->CompLabel->Size = System::Drawing::Size(88, 13);
this->CompLabel->TabIndex = 3;
this->CompLabel->Text = L"Computer Choice";
//
// WonLabel
//
this->WonLabel->AutoSize = true;
this->WonLabel->Location = System::Drawing::Point(91, 163);
this->WonLabel->Name = L"WonLabel";
this->WonLabel->Size = System::Drawing::Size(62, 13);
this->WonLabel->TabIndex = 3;
this->WonLabel->Text = L"Who Won\?";
//
// Form1
//
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->WonLabel);
this->Controls->Add(this->CompLabel);
this->Controls->Add(this->WonBox);
this->Controls->Add(this->ComBox);
this->Controls->Add(this->cis);
this->Controls->Add(this->Paper);
this->Controls->Add(this->Rock);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
int userChoice, compChoice;
private: System::Void Rock_Click(System::Object^ sender, System::EventArgs^ e)
{
ComBox->Text="";
userChoice=1;
compChoice=rand()%3+1;
switch(compChoice)
{
case 1:
ComBox->Text="Rock";
WonBox->Text="You Tied";
break;
case 2:
ComBox->Text="Paper";
WonBox->Text="You Lost";
break;
case 3:
ComBox->Text="Scisors";
WonBox->Text="You Won";
break;
}
}
private: System::Void Paper_Click(System::Object^ sender, System::EventArgs^ e) {
ComBox->Text="";
userChoice=2;
compChoice=rand()%3+1;
switch(compChoice){
case 1:
ComBox->Text="Rock";
WonBox->Text="You Won";
break;
case 2:
ComBox->Text="Paper";
WonBox->Text="You Tied";
break;
case 3:
ComBox->Text="Scisors";
WonBox->Text="You Lost!";
break;
}
}
private: System::Void cis_Click(System::Object^ sender, System::EventArgs^ e) {
ComBox->Text="";
userChoice=3;
compChoice=rand()%3+1;
switch(compChoice){
case 1:
ComBox->Text="Rock";
WonBox->Text="You Lost";
break;
case 2:
ComBox->Text="Paper";
WonBox->Text="You Won";
break;
case 3:
ComBox->Text="Scisors";
WonBox->Text="You Tied!";
break;
}
} //The culprit!
};
}
I've only added one bracket near the end,closing the last method in class. Since Visual Studio 2010 and earlier don't support the IntelliSense in C++/CLI, bugs are likely to occur in such projects.
I am creating an application where the front end has to be a Windows Form using C++/CLI. The form is used for login purpose.
In my form, I have a register button. On click of this button, a new form should open ( closing the login form ). I was able to achieve this by the following code:
Form^ rgForm = gcnew RegisterForm;
rgForm->Show();
this->Hide(); // using this->Close() was closing the application
Now I want to have a cancel button on the register form, whose click should open the login form again and close the register form. How do I achieve that?
( I am confused with the use of this->Hide(), does it mean that the form exists, we just did not show it, and so even after the register form visibility, the login form still exists? )
Update : Now current form handle is passed into register form constructor ( storing it as a private variable with the name loginForm in RegisterForm class ).
Following is the code for cancel button click:
// RegisterForm class constructor
RegisterForm(System::Windows::Forms::Form^ f)
{
loginForm = f;
}
// Cancel button click
private: System::Void BtnCancel_Click(System::Object^ sender, System::EventArgs^ e)
{
loginForm->Show();
this->Hide();
}
On cancel button click I am getting the exception : "object not set to instance".
Can someone please help me.
Thanks.
Create a constructor for RegisterForm which accepts a System::Windows::Form ^ object, and pass this to it when you are instantiating it from within the login form class
Form^ rgForm = gcnew RegisterForm(this);
rgForm->Show();
this->Hide();
Assume the login form object is called otherform within the RegisterForm class. Once you're ready to bring it back, just call otherform->Show()
When you hide the form, it still exists, it is just not visible to the user.
EDIT: I got this to work just fine. Here are the modifications (not the full code) that I made to the form
Form2(Registration form)
Form2(System::Windows::Forms::Form ^ frm1)
{
otherform = frm1;
InitializeComponent();
}
private: System::Windows::Forms::Form ^ otherform;
private: System::Void Cancel_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
otherform->Show();
}
Form1(Login form)
#include "Form2.h"
private: System::Void Register_Click(System::Object^ sender, System::EventArgs^ e) {
Form2 ^ frm2 = gcnew Form2(this);
frm2->Show();
this->Hide();
}
Thereby , I would like to show my full code to help the others:-
Form2.h
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace Form1 {
/// <summary>
/// Summary for Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
private: System::Windows::Forms::Form ^ otherform;
public:
Form2(System::Windows::Forms::Form ^ o )
{
otherform = o;
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
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(147, 132);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(102, 38);
this->button1->TabIndex = 0;
this->button1->Text = L"Menu";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(412, 295);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
otherform->Show();
}
};
////////////////////////
}
///////////////////////Form1.h
#pragma once
#include "stdafx.h"
#include "Form2.h"
namespace Form1 {
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(140, 206);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Login";
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(377, 291);
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) {
Form2 ^ frm2 = gcnew Form2(this);
frm2->Show();
this->Hide();
}
};
}