Reputation: 41
I have been trying to make Form1
visible from Form2
. What happens in my program is that you press a button in form1
to hide form1
, then Form2
will pop up... But when a timer runs out on Form2
, It is supposed to hide itself and Form1
is supposed to pop back up.
But When I try and run I get this:
1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form2.h(122): error C2039: 'Form1' : is not a member of 'Retaliation::Form2'
(Ps. Retaliation is the name of the program).
Here is my code (Which has #include "Form1.h"
at the top)
#pragma once
#include "stdAfx.h"
#include "Form1.h"
namespace Retaliation {
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;
using namespace std;
/// <summary>
/// Summary for Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
public:
int MoviePlay;
public:
int TickL;
Form2(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
private: AxWMPLib::AxWindowsMediaPlayer^ MoviePlayer1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
protected:
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)
{
this->components = (gcnew System::ComponentModel::Container());
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form2::typeid));
this->MoviePlayer1 = (gcnew AxWMPLib::AxWindowsMediaPlayer());
this->timer1 = (gcnew System::Windows::Forms::Timer(this- >components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ > (this->MoviePlayer1))->BeginInit();
this->SuspendLayout();
//
// MoviePlayer1
//
this->MoviePlayer1->Enabled = true;
this->MoviePlayer1->Location = System::Drawing::Point(0, -1);
this->MoviePlayer1->Name = L"MoviePlayer1";
this->MoviePlayer1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^ >(resources- >GetObject(L"MoviePlayer1.OcxState")));
this->MoviePlayer1->Size = System::Drawing::Size(860, 547);
this->MoviePlayer1->TabIndex = 0;
this->MoviePlayer1->Enter += gcnew System::EventHandler(this, &Form2::axWindowsMediaPlayer1_Enter);
//
// timer1
//
this->timer1->Interval = 1000;
this->timer1->Tick += gcnew System::EventHandler(this, &Form2::timer1_Tick);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(859, 495);
this->Controls->Add(this->MoviePlayer1);
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->MoviePlayer1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void axWindowsMediaPlayer1_Enter(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
String^ Movie = File::ReadAllText("Movie.txt");
if (Movie == "1")
{
MoviePlayer1 -> Ctlcontrols -> play();
}
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
TickL += 1;
if (Movie == "1")
{
if (TickL == 47)
{
this -> timer1 -> Stop();
TickL = 0;
this -> Hide();
this -> Form1 -> Show();
}
}
}
};
}
Upvotes: 2
Views: 3144
Reputation: 1
While calling form2 from form1 hide form1 and then call form2. After calling form2 unhide form1.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Visible = false;
Form2^ f2 = gcnew Form2;
f2->ShowDialog();
this->Visible = true;
}
};
Upvotes: 0
Reputation: 26259
What you need to do is to pass a reference to Form1
at the time it creates and shows Form2
. Then, when Form2
closes, it can use that reference to show Form1
again.
So change your Form2
to have a private
member variable to reference Form1
and set this in the constructor
public ref class Form2 : public System::Windows::Forms::Form
{
private:
Form1 ^m_pMainForm;
public:
Form2(Form1 ^pMainForm)
{
InitializeComponent();
// save reference to main form
m_pMainForm = pMainForm;
}
// ... etc.
Then change the code in Form1
that creates Form2
to pass a reference to itself
public SomeMethodInForm1()
{
// create and show Form2
Form2 ^pFormTwo = gcnew Form2(this); // pass a reference to Form1
pFormTwo->Show();
}
Finally, in Form2
, you can use the saved reference to redisplay Form1
public SomeMethodInForm2()
{
// redisplay Form1
m_pMainForm->Show();
// ... close Form2 or whatever you want to do
}
Depending on the order of your class declarations, you may need to add a forward declaration for one of the forms, or maybe split the code appropriately between header and definition, but that's a small detail. Just post another question if you're still stuck.
Upvotes: 2