Really Odd Windows Form C++ Error - winforms

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.

Related

vc++ 2015 The use of RichTextBox

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.

C++ .NET VisibleChanged Called if parent is set visible, but not if parent is set not visible

I got a form containing a panel, that itself contain a label. A attach to the VisibleChanged of the label. I add two button to the form. One that set the Visible of the Panel to true, and the other that set the same property to false.
When I click the visible button, the VisibleChanged event is thrown. When I hide the panel, the event is not launched. What am I doing wrong.
Here is the code of my form.
#pragma once
namespace CLRConsoleApplication1 {
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 MyForm
/// </summary>
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::Panel^ panel1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ ShowButton;
private: System::Windows::Forms::Button^ HideButton;
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->panel1 = (gcnew System::Windows::Forms::Panel());
this->label1 = (gcnew System::Windows::Forms::Label());
this->ShowButton = (gcnew System::Windows::Forms::Button());
this->HideButton = (gcnew System::Windows::Forms::Button());
this->panel1->SuspendLayout();
this->SuspendLayout();
//
// panel1
//
this->panel1->Controls->Add(this->label1);
this->panel1->Location = System::Drawing::Point(32, 32);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(200, 108);
this->panel1->TabIndex = 0;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(60, 38);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"label1";
this->label1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);
//
// ShowButton
//
this->ShowButton->Location = System::Drawing::Point(32, 162);
this->ShowButton->Name = L"ShowButton";
this->ShowButton->Size = System::Drawing::Size(75, 23);
this->ShowButton->TabIndex = 1;
this->ShowButton->Text = L"Show";
this->ShowButton->UseVisualStyleBackColor = true;
this->ShowButton->Click += gcnew System::EventHandler(this, &MyForm::ShowButton_Click);
//
// HideButton
//
this->HideButton->Location = System::Drawing::Point(157, 162);
this->HideButton->Name = L"HideButton";
this->HideButton->Size = System::Drawing::Size(75, 23);
this->HideButton->TabIndex = 2;
this->HideButton->Text = L"Hide";
this->HideButton->UseVisualStyleBackColor = true;
this->HideButton->Click += gcnew System::EventHandler(this, &MyForm::HideButton_Click);
//
// MyForm
//
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->HideButton);
this->Controls->Add(this->ShowButton);
this->Controls->Add(this->panel1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->panel1->ResumeLayout(false);
this->panel1->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void label1_VisibleChanged(System::Object^ sender, System::EventArgs^ e) {
System::Windows::Forms::MessageBox::Show( "label1_VisibleChanged: " + label1->Visible );
}
private: System::Void HideButton_Click(System::Object^ sender, System::EventArgs^ e) {
panel1->Visible = false;
}
private: System::Void ShowButton_Click(System::Object^ sender, System::EventArgs^ e) {
panel1->Visible = true;
}
};
}
PS. I am using Visual Studio 2012 Update 4.
Edit: Here is bit more information. I have access to a control. Another project have access to the same control, and put in inside a panel inside a form. Then then hide or show the panel depending on user request.
On my side, I don't know the panel, only the control. I want to know when the control become visible or hidden, because I need to change some related control accordingly.
Right now you have
label1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);
add also
panel1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);
and see if you get the desired results.

Can't pass a Windows Form to another via constructor

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

A Simple GUI do not work. [C++]

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

Calling one win form from another win form

I have 2 win forms built using Managed C++. On press of a button another form (login.h) should be opened from Client.h, but I am getting error like "error C3673: 'SenderApp::Login' : class does not have a copy-constructor".
Below is the login.h code:
#pragma once
namespace SenderApp {
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 Login
/// </summary>
public ref class Login : public System::Windows::Forms::Form
{
public:
Login(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Login()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label4;
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->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->label4 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 111);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(60, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"User Name";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(136, 21);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(58, 13);
this->label2->TabIndex = 1;
this->label2->Text = L"User Login";
this->label2->Click += gcnew System::EventHandler(this, &Login::label2_Click);
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(12, 157);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(53, 13);
this->label3->TabIndex = 2;
this->label3->Text = L"Password";
this->label3->Click += gcnew System::EventHandler(this, &Login::label3_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(91, 108);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(181, 20);
this->textBox1->TabIndex = 3;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(91, 154);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(181, 20);
this->textBox2->TabIndex = 4;
this->textBox2->TextChanged += gcnew System::EventHandler(this, &Login::textBox2_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(63, 212);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 5;
this->button1->Text = L"Submit";
this->button1->UseVisualStyleBackColor = true;
//
// button2
//
this->button2->Location = System::Drawing::Point(197, 212);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 6;
this->button2->Text = L"Cancel";
this->button2->UseVisualStyleBackColor = true;
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(12, 59);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(303, 13);
this->label4->TabIndex = 7;
this->label4->Text = L"[Please use the pre-defined logins in login.xml at C:\\Repository]";
this->label4->Click += gcnew System::EventHandler(this, &Login::label4_Click);
//
// Login
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(339, 267);
this->Controls->Add(this->label4);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Name = L"Login";
this->Text = L"Login";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void label2_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label4_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}
I tried something like Login lgn = gcnew Login();
I know this question is really old, but I found it having the same problem and it was no help, so when I found my solution I figured I should post it...
You don't have to implement a copy constructor, you can just use a pointer, i.e. -
Login^ lgn = gcnew Login();
lgn->Show();

Resources