Calling one win form from another win form - winforms

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();

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.

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

Really Odd Windows Form C++ Error

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.

C++/CLI - how to open a new form and back

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();
}
};
}

Resources