Access Form1 MenuStripItem from Form 2

closed account (Shp21hU5)
Hi Guys, I'm using visual studio 2019.
what I'm trying to do is call and access a menu option in my mainform called Form1 from Form2.

I've looked on the net and tried loads of example to no avail.

i've included "#include Form1.h" in form2 and I've also added "ref class Form1" above "public ref class Form2 : public System::Windows::Forms::Form"

The code calls the menuItem fine and reads the code within it, but does nothing !
yet if I click on the menuItem from Form1 it works fine.


Code extract example.

//Form1 code.
private: System::Void HidButtonMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

//Rem:: Hide Toolbar Button.
toolStripButton1->Visible = false;
}
This is C++/cli. Uhh...
Probably need to show more than two lines of code. Also, as seeplus mentioned, not many people here actively use C++\CLI, so you're less likely to get help, but I could still try.
closed account (Shp21hU5)
Hi Ganado

if you don't mind me asking but why don't many people use C++\Cli ?

anyway here's my code. Note I've had to remove some of the code because I exceeded my length limit.

/* MyForm.cpp */
#pragma once

#include "MyForm.h"
#include "Form2.h"

using namespace TestForm2;


[STAThread]

int main(array<System::String^>^ args)
{

Application::EnableVisualStyles();

Application::SetCompatibleTextRenderingDefault(false);

//MyProject::MyForm form;

// Application::Run(% form);

Application::Run(gcnew MyForm());

return 0;

}



/* MyForm.h */


#pragma once
#include "MyForm.h"
#include "Form2.h"



private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {


Form2^ frm = gcnew Form2();

frm->Show();

}
public: System::Void toolStripButton1_Click(System::Object^ sender, System::EventArgs^ e) {

toolStripButton1->Visible = false;
}

public: System::Void hideButtonToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

toolStripButton1->Visible = false;
}
private: System::Void callForm2ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

Form2^ frm = gcnew Form2();

frm->Show();
}
private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

Close();
}
};
}


/* Form2.cpp */

#pragma once
#include "MyForm.h"
#include "Form2.h"


namespace TestForm2 {


void Form2::OnClickFunc()
{

MyForm^ Form1 = gcnew MyForm();


System::Object^ Sender = gcnew Object; // MyForm();

System::EventArgs^ e = gcnew EventArgs;


Form1->hideButtonToolStripMenuItem_Click(Sender, e);

}

}


/* Form2.h */

#pragma once

namespace TestForm2 {

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 Form2
/// </summary>
///
ref class MyForm;

public ref class Form2 : public System::Windows::Forms::Form
{


public:
// Add a member function
void OnClickFunc();

public:
Form2(void)
{

//
InitializeComponent();
//
//TODO: Add the constructor code here
//

}



protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
OnClickFunc();
delete components;
}


}

private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ closeForm2ToolStripMenuItem;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem1;
private: System::Windows::Forms::ToolStripMenuItem^ closeToolStripMenuItem;
protected:

//private: System::Void Form2::acccc(System::Object^ sender, System::EventArgs^ e);

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->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->closeForm2ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->closeForm2ToolStripMenuItem });
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
this->fileToolStripMenuItem->Text = L"File";
//
// closeForm2ToolStripMenuItem
//
this->closeForm2ToolStripMenuItem->Name = L"closeForm2ToolStripMenuItem";
this->closeForm2ToolStripMenuItem->Size = System::Drawing::Size(140, 22);
this->closeForm2ToolStripMenuItem->Text = L"Close Form2";
this->closeForm2ToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::closeForm2ToolStripMenuItem_Click);
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->fileToolStripMenuItem1 });
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(605, 24);
this->menuStrip1->TabIndex = 0;
this->menuStrip1->Text = L"menuStrip1";
//
// fileToolStripMenuItem1
//
this->fileToolStripMenuItem1->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->closeToolStripMenuItem });
this->fileToolStripMenuItem1->Name = L"fileToolStripMenuItem1";
this->fileToolStripMenuItem1->Size = System::Drawing::Size(37, 20);
this->fileToolStripMenuItem1->Text = L"File";
//
// closeToolStripMenuItem
//
this->closeToolStripMenuItem->Name = L"closeToolStripMenuItem";
this->closeToolStripMenuItem->Size = System::Drawing::Size(180, 22);
this->closeToolStripMenuItem->Text = L"Close";
this->closeToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form2::closeToolStripMenuItem_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(605, 325);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form2";
this->Text = L"Form2";
this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form2::Form2_FormClosed);
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) {

OnClickFunc();


} private: System::Void closeForm2ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

OnClickFunc();
}



private: System::Void Form2_FormClosed(System::Object^ sender, System::Windows::Forms::FormClosedEventArgs^ e) {

OnClickFunc();
}
private: System::Void closeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {

this->Close();
}
};
}
if you don't mind me asking but why don't many people use C++\Cli ?

To be blunt, I do not use it because it is a screwy syntax extension attempting to solve a nonexistent problem that exists because microsoft felt a total do-over was better than simply updating and fixing MFC and, IMHO, because M$ wants you to use C# instead.

If they had not decided to mangle it, I would have used it. The xor stuff + the weird dynamic memory stuff is just pointless -- they could have hidden the dynamic memory instead of forcing use of garbage collection. All they really had to do there was use .. smart pointers, which the language supports.
Last edited on
Jarv,
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Reading a huge wall of text without formatting and indentation makes getting a tooth extracted without anesthetic a less painful experience.

Why don't more programmers use C++/CLI? MS bloat and unneeded mangling of C++. The original WinAPI was C-based, and was somewhat of a nightmare to learn since the API was so huge. C++/CLI is IMO worse.

MS did the same thing with BASIC, turning it into Visual Basic. A Frankenstein's Monster that wasn't easy to learn and a PITA to do anything. It was the all the less than perfect C-based WinAPI ideas on steroids.

C# is yet another "we think we can do better, but it will end up making things worse" MS innovation that coddles and hobbles a programmer unnecessarily. Good programmers don't need to have their hand held by garbage collection, etc. Mediocre and bad programmers will find ways to trash memory despite it.
closed account (Shp21hU5)
Thank you guy's for all your help.

I think i've have found away round my problem now but will need to modify my code now to suit. How long it takes me find solutions when I get stuck I think I might go back to my borland C++ instead because I'm only an amature anyway.

once again guy's thank you.
C++/CLI is NOT ISO Standard C++. Never was, never will be.

I'm a hobbyist myself, and I prefer using tools that are as up-to-date, C++20 currently, as possible. So I use VS 2019/2020, Code::Blocks with a tweak to use an easily updated compiler, MSYS 2. 2 IDEs and one command-line compiler set-up.

Using Borland C++ will restrict severely what you can use in the C++ toolbox.
if you want to use the CLI, use it. it works, and there are examples and help online. Its just not popular; if it were anything other than microsoft's junk it would have 0 people using it (apart from its author and his best friend). Because it is microsoft, though, people use it and support/help/examples/etc exist, and M$ is usually fairly good with documentation as well.

Borland has been out of the game for decades. Builder is being kept up, sort of, if you can find a reasonably modern build for it that would be "ok". Its a very easy gui builder.

Actually I think its the GDI? CLI is usually 'command line interface', GDI is one of visual studio's windowing systems, and the managed gibberish .. I don't know its official name apart from 'managed'. If they called the managed GUI stuff CLI, ... sigh ..
Last edited on
Borland has morphed into Embarcadero - and there is still Delphi and C++! It's 'advantage' are the VCL components (based upon the old 'OWL'). The C++ compiler is based upon Clang and there are also some free versions for non-commercial usage.

https://www.embarcadero.com/free-tools
C++/cli is MS's second attempt to merge C++ with .net (the previous version was just called Managed C++). It's Herb Sutter's baby - he did the design etc. And in IMO got it wrong. All that 'boxing' with ^ etc for the 'managed variables' et al. If you want to use .net then use c# (or vb.net if that's your tipple). We looked at it a while ago and basically said 'yuk'.

There's also C++/WinRT which is MS's attempt to merge C++ with 'Universal Windows Platform [UWP]'. OMG! https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt


Topic archived. No new replies allowed.