'how can i use a function from MyForm1.h in MyForm.h
i have a funcion 'button1_click' in the header 'MyForm1.h'. I want to use this function in another header 'MyForm.h'. How can i do this? i already included 'MyForm1.h' in 'MyForm.h'.
Solution 1:[1]
It took me some time to complete an example that does what you want. You need to define a button property, I defined this property in MyForm1.h
, please read the following code carefully.
#pragma once
namespace Project7 {
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 MyForm1
/// </summary>
public ref class MyForm1 : public System::Windows::Forms::Form
{
public:
MyForm1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
property Button^ Buttontest {
Button^ get() {
return button1;
}
void set(Button^ value) {
button1 = value;
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm1()
{
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(164, 90);
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, &MyForm1::button1_Click);
//
// MyForm1
//
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"MyForm1";
this->Text = L"MyForm1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show("sda");
}
};
}
In MyForm.h
, you need two buttons, one is used to display MyForm1
, the other is used to call the click event of MyForm1
, please read the following code carefully.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
MyForm1^ form1 =(MyForm1^) Application::OpenForms["MyForm1"];
form1->Buttontest->PerformClick();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
MyForm1^ form1 = gcnew MyForm1;
form1->Show();
}
};
Using the above code you have to make sure you use button2 to show another window first and then use button1 to call the click event.
Solution 2:[2]
If you have added the reference into the form file then you should be able to call it from MyForm.h:
#include "MyForm1.h"
namespace MyForms {
... Initialize component and winform code ...
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
MyForm1^ form1 = gcnew MyForm1;
form1->ShowDialog();
lblData->Text = form1->MyFunctionFromForm1();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yujian Yao - MSFT |
Solution 2 | Armando Bracho |