Unexpected end-of-file in visual form - c++

Getting error, when trying to run code. I am using Visual Forms. VS 2013
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Project1::MyForm form;
Application::Run(%form);
return 0;
}
MyForm.cpp(17): fatal error C1004: unexpected end-of-file found
MyForm.h:
#pragma once
namespace Project1 {
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:
/// <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->SuspendLayout();
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
MyForm.cpp(17): fatal error C1004: unexpected end-of-file found

I just missed something in the code.
#include "MyForm.h"
using namespace System;
using namespace System ::Windows::Forms;
[STAThread]
void main(array<String^>^ arg) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Project1::MyForm form;
Application::Run(%form);
}

Related

MSB4236 The SDK Microsoft.NET.Sdk specified could not be found

I've created a CLR (.NET Core) project and added a Windows Form there. I didn't change any code and tried to run the program. Then the error MSB4236 The SDK Microsoft.NET.Sdk specified could not be found comes in.
enter image description here
The header file code:
`#pragma once
namespace Project21 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
protected:
~MyForm()
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->Size = System::Drawing::Size(300,300);
this->Text = L"MyForm";
this->Padding = System::Windows::Forms::Padding(0);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
}
#pragma endregion
};
} `

Creating std::thread in Class with Header

How do I initialize DefenseThread so that it will start executing Defend()? I have seen a lot of examples on how to do it from a .cpp file without a header, but non with.
void CommandDefend::DefendStart()
Gets the following error:
'&' requires I-value
Header:
#pragma once
#include <thread>
class CommandDefend : public ICommand
{
public:
CommandDefend();
~CommandDefend();
private:
thread* DefenseThread;
/// <summary>
/// Starts the commands execution.
/// </summary>
void DefendStart();
/// <summary>
/// Does the actual defending.
/// </summary>
void Defend();
};
CPP:
#include "stdafx.h"
#include "CommandDefend.h"
void CommandDefend::DefendStart()
{//ERROR HERE!!!!!!
DefenseThread = new thread(&CommandDefend::Defend);
}
void CommandDefend::Defend()
{
}
If you replace
DefenseThread = new thread(&CommandDefend::Defend);
with
DefenseThread = new thread(&CommandDefend::Defend, this);
it should work, because it's the way how I initialize class members of type std::thread (while they are not pointers).

How to invoke/call performClick of menuitem of an MDI parent from another form?

I have a problem, sorry for this newbie question (just a 2 week visual c++ programming experience). I am currently working with MDI in Visual C++2010, here is how the program works, once you execute the application, the MDIParent loads in maximize state, then the MDIParent has MenuStrip and one of its subMenu is named noviceToolStripMenuItem. If noviceToolStripMenuItem is clicked using the mouse button, it will open a child form called frmNovice, this works out fine, now the frmNovice contains a simple game which works out according to its intended function, once the game is over a new form named frmRetry will be shown but this time frmRetry is not a childform and the frmNovice will close. frmRetry has two buttons, Yes and No, if the user clicks the No the frmRetry will close and the MDIParent will then again holds the focus of the application, but if the user clicks the Yes button, the application should perform the click event/function of noviceToolStripMenuItem, to show up again the frmNovice, but this is the problem, i can't get this done. frmRetry (not a child form) to invoke or call the click event noviceToolStripMenuItem of the MDIParent. Is there any way to do this, or maybe other solution? thanks in advance.
This are the codes (some are deleted to minimize space):
**FILE: mdiMain.h**
#pragma once
#include "frmNovice.h"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ noviceToolStripMenuItem;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->selectLevelToolStripMenuItem = (gcnew
System::Windows::Forms::ToolStripMenuItem());
this->noviceToolStripMenuItem = (gcnew
this->selectLevelToolStripMenuItem->Name = L"selectLevelToolStripMenuItem";
this->selectLevelToolStripMenuItem->Size = System::Drawing::Size(70, 20);
this->selectLevelToolStripMenuItem->Text = L"New &Game";
//
// noviceToolStripMenuItem
//
}
#pragma endregion
private: System::Void noviceToolStripMenuItem_Click(System::Object^ sen....) {
//OPENING NEW FORM AS MDI CHILD
frmNovice^ newMDIChild = gcnew frmNovice();
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
};
}
FILE: mdiMain.cpp
#include "stdafx.h"
#include "mdiMain.h"
using namespace MemoryGame;
[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;
}
**FILE: frmNovice.h**
#pragma once
#include "frmRetry.h"
#include "algorithm"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmNovice : public System::Windows::Forms::Form
{
public:
frmNovice(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~frmNovice()
{
if (components)
{
delete components;
}
}
//codes for the UI of the game goes here.....
#pragma region Windows Form Designer generated code
//MY USER DEFINED FUNCTION START
void gamefunction() //this function is where calls the frmRetry to show
{
frmRetry^ form = gcnew frmRetry();
form->Show(); //This time not a CHILD FORM
this->Close();
}
void InitializeComponent(void)
{
//initialize UI components GOES HERE
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//DIFFERENT PROCEDURE FUNCTIONS GOES HERE Also invokes/calls the game function that
//show the frmRetry (this is working properly)
};
}
FILE: frmNovice.cpp
#include "StdAfx.h"
#include "frmNovice.h"
//there are variables used for gameplay
int Choice1;
int Choice2;
int Mistakes;
int TimeRecord;
int AllItems;
and LASTLY
FILE: frmRetry.h
#pragma once
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmRetry : public System::Windows::Forms::Form
{
`enter code here`public:
frmRetry(void)
{
InitializeComponent();
}
protected:
~frmRetry()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ btnYes;
private: System::Windows::Forms::Button^ btnNo;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
CODES for UI inialization goes here
}
#pragma endregion
private: System::Void btnNo_Click(System::Object^ .....e) {
this->Close();
}
private: System::Void btnYes_Click(System::Object.....^ e) {
**THIS IS THE PART IM STuck WITH, i CANT GET THIS DONE**
**EVErything is working fine, except for this part where i have to
call again the frmNovice as an child form of the mdiParent**
THE following are my attempts but didnt work.
// frmRetry^ form = gcnew frmRetry();
// frmNovice^ frmNovice=gcnew frmNovice();
// noviceToolStripMenuItem->PerformClick();
//frmNovice->Show();
}
};
}
FILE: frmRetry.cpp
#include "StdAfx.h"
#include "frmRetry.h"
THANKS IN ADVANCE
You should pass in a reference to the main window when you create the Novice form, and pass this reference again to the Retry form. Then, have the main window implement a NoviceParent interface, which includes a public method for the Retry form to call. The noviceToolstripMenuItem_Click method on the main window will also delegate to this new public method.
Novice Parent Interface
public interface class INoviceParent
{
public:
void ShowNovice();
}
Main Window
#include "INoviceParent.h"
public ref class Form1 : public System::Windows::Forms::Form, INoviceParent {
public:
// ... existing public methods
void ShowNovice() {
//OPENING NEW FORM AS MDI CHILD
frmNovice^ newMDIChild = gcnew frmNovice(this);
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
private:
void noviceTooltipMenuItem_Click( /* args */) {
ShowNovice();
}
// ...
};
Novice Form
#include "INoviceParent.h"
public ref class frmNovice : public System::Windows::Forms::Form
{
public:
frmNovice(INoviceParent ^ const parent)
{
this->parent = parent;
// ...
}
// ...
private:
void gamefunction() //this function is where calls the frmRetry to show
{
frmRetry^ form = gcnew frmRetry(parent);
form->Show(); //This time not a CHILD FORM
this->Close();
}
INoviceParent ^parent;
}
Retry Form
#include "INoviceParent.h"
public ref class frmRetry : public System::Windows::Forms::Form
{
public:
frmRetry(INoviceParent ^ const noviceParent)
{
this->noviceParent = noviceParent;
// ...
}
// ...
private: System::Void btnYes_Click(System::Object.....^ e) {
noviceParent->ShowNovice();
}
INoviceParent ^noviceParent;
};
The important thing here is that the sub-forms don't need to be strongly coupled to the main form or to each other. All they know is that the object passed in their constructor has a public method called ShowNovice(). The Retry form calls it when the user clicks the "Yes" button, and the main window does whatever it does when the user clicks its noviceTooltipMenuItem.

Change a forms "visible" property from another function?

I need to change testAppGUI's (testAppGUI is a form) visible property from another function. The function is in a separate file and it's not in a class.
If I try to do
testAppGUI::Visible = false;
I just get error
C2597: illegal reference to non-static member 'System::Windows::Forms::Control::Visible'
And if I try create a instance of the object like this
testAppGUI^ formProperty = gcnew testAppGUI;
and then do
formProperty->Visible = false; nothing happens?!
Can anybody explain how to do this?
Thanks in advance.
EDIT: Here is some more code
In testApp.cpp
#include "stdafx.h"
#include "testAppGUI.h"
using namespace testApp;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew testAppGUI());
return 0;
}
In testAppGUI.h
#pragma once
#include "HideAndShowGUI.h"
namespace testApp {
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 System::IO;
public ref class testAppGUI : public System::Windows::Forms::Form
{
public:
testAppGUI(void)
{
InitializeComponent();
}
protected:
~testAppGUI()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
...
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
...
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
hideGUI();
}
};
}
HideAndShowGUI.cpp
#include "stdafx.h"
#include "testAppGUI.h"
using namespace testApp;
void hideGUI(){
//Hide the form, this function should be able to be called by all functions in the program. Not just from forms!
}
void showGUI(){
//Unhide/Show the form, this function should be able to be called by all functions in the program. Not just from forms!
}
hideGUI and showGUI is declared in HideAndShowGUI.h
If you already have an instance of the form which you want to hide, you will have to pass a reference to that form to the function where you want to change the property.
You can do this either by directly supplying the form as a parameter of the function or if the function is a member of a class, you can pass the form to (and instance of) the class (and store it as a member variable). Which of these is more appropriate for you depends on your specific context, which we do not have access to without some more of your code.
Note: Your first snipet is conflicting with your second: in the first you are using form1 as a variable, in the second as a type. If you already have the variable form1, you can just set its property:
form1->Visible = false;

Trying to run a thread from a form

I have two related qestions on the code included below
1) I am trying to read from a serial port that is part of a Visual C++ Form. I want to create a thread in the InitializeComponent function but I get this error on the form page when I include the call to start the thread:
"Warning 1 Could not find type 'Thread'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built."
2) The thread will run in the static function Read. Read needs to resolve the serial port that is in the main form (serial port is named arduino),
but it apparently can't resolve them: "left of .ReadLine' must have class/struct/union"
Suggestions?
using namespace System::IO::Ports;
using namespace System::Threading;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
private: void static Read(void)
{
while (1)
{
try
{
String^ message = arduino.ReadLine();
// this->ArduinoOutputTextBox->Text = message;
}
catch (TimeoutException ^) { }
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ USB_button;
private: System::IO::Ports::SerialPort^ arduino;
private: System::Windows::Forms::TextBox^ ArduinoOutputTextBox;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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)
{
Thread^ readThread = gcnew Thread(gcnew ThreadStart(Read));
this->components = (gcnew System::ComponentModel::Container());
this->USB_button = (gcnew System::Windows::Forms::Button());
this->arduino = (gcnew System::IO::Ports::SerialPort(this->components));
this->ArduinoOutputTextBox = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
arduino is a reference to an object, not an actual object.
You ned to write arduino->readLine().