65.9K
CodeProject 正在变化。 阅读更多。
Home

使用事件和委托在窗体之间传递数据 (C++/CLI)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.91/5 (5投票s)

2016 年 3 月 7 日

CPOL

2分钟阅读

viewsIcon

28790

downloadIcon

723

一个 Visual Studio 2015 项目, 展示了一种在 Windows 窗体之间传递信息的方法。

引言

这个简单的项目包含一个主(父)窗体和两个子窗体。

主窗体向每个子窗体传递一个string

每个子窗体向主窗体以及另一个子窗体传递一个string

目前还不清楚如何直接从一个子窗体向另一个子窗体传递string。因此,我的解决方案是每个子窗体都将string传递给主窗体,然后主窗体再将string传递给另一个子窗体。

欢迎提供任何无需此中继机制的解决方案。

背景

我编程多年,但从未遇到过需要在窗体之间传递数据的需求,除非通过引用传递对象。

当我听说委托时,我搜索了简单的例子,但大多数例子都包含了多余的代码。

希望我的例子能帮助你理解基础知识。

Using the Code

**主窗体**包含对每个子窗体的引用

#include "ChildForm1.h"
#include "ChildForm2.h"
private: ChildForm1^ c1;
private: ChildForm2^ c2;

在主窗体的加载方法中,创建每个子窗体的对象

this->c1 = gcnew ChildForm1();
this->c2 = gcnew ChildForm2();

以下显示了如何将string直接从主窗体发送到每个子窗体——这里没有什么神奇之处——不需要委托。

每个子窗体中的SetTextBox方法必须声明为public

private: System::Void sendButton1(System::Object^  sender, System::EventArgs^  e) 
{
	this->c1->SetTextBox1(this->textBox3->Text);
}
private: System::Void sendButton2(System::Object^  sender, System::EventArgs^  e) 
{
	this->c2->SetTextBox1(this->textBox4->Text);
}

接下来,主窗体订阅(作为监听器)子窗体的“发送文本”发布事件

+=”表示我们正在订阅。您可以随时使用“-=”取消订阅。

请注意,下面的第二个参数必须是主窗体事件处理程序的完整规范。不要忘记在命名空间前添加“&”(& + 命名空间 + 订阅者的窗体名称 + 订阅者的事件处理程序)。

我使用了4个监听器——2个用于从每个子窗体到父窗体的string,以及2个用于从每个子窗体到另一个子窗体的string(通过父窗体中转)。

// Events have only 2 args
this->c1->myEvent1 += gcnew ChildForm1::EventDelegate1
(this, &PassingDataBetweenForms::Form1::mySubscriber1a);
this->c1->myEvent2 += gcnew ChildForm1::EventDelegate2
(this, &PassingDataBetweenForms::Form1::mySubscriber1b);
this->c2->myEvent1 += gcnew ChildForm2::EventDelegate1
(this, &PassingDataBetweenForms::Form1::mySubscriber2a);
this->c2->myEvent2 += gcnew ChildForm2::EventDelegate2
(this, &PassingDataBetweenForms::Form1::mySubscriber2b);

以下是上面声明的主窗体的订阅者事件处理程序。当子窗体发布事件时,string将显示在目标窗体的TextBox中。

private: void mySubscriber1a
(System::Object^  sender, System::EventArgs^  e, String^ text) // From child form 1
{
	this->textBox1->Text = text; // Here's where we display the message from child1 to parent
}
private: void mySubscriber1b
(System::Object^  sender, System::EventArgs^  e, String^ text) // From child form 1
{
	this->c2->SetTextBox2(text); // Here's where we forward the message from child2 to child1
}
private: void mySubscriber2a
(System::Object^  sender, System::EventArgs^  e, String^ text) // From child form 2
{
	this->textBox2->Text = text; // Here's where we display the message from child2 to  parent 
}
private: void mySubscriber2b
(System::Object^  sender, System::EventArgs^  e, String^ text) // From child form 2
{
	this->c1->SetTextBox2(text); // Here's where we forward the message from child1 to child2
}

**子窗体**负责定义(public)委托和事件,以便发布订阅者(主窗体)监听的string

在这种情况下,一个事件将string发送给其父级,另一个事件将string发送给其兄弟(通过父级)。

// Publish an event (with a text message) that other forms can subscribe to
public: delegate void EventDelegate1(System::Object^ sender, System::EventArgs^ e, String^ message);
public: event EventDelegate1^ myEvent1;
public: delegate void EventDelegate2(System::Object^ sender, System::EventArgs^ e, String^ message);
public: event EventDelegate2^ myEvent2;

请注意,在任何子窗体中都没有#include(引用另一个子窗体),因为这将创建一个循环引用,无法编译。

因此,当单击子窗体的发送按钮时,它将触发其事件处理程序之一。

public: void issueEvent1(System::Object^  sender, System::EventArgs^  e)	//This is where 
                                                               //we publish the event to all subscribers
{
	this->myEvent1(this, e, this->textBox3->Text); // Raise the event using the EventDelegate signature
}
public: void issueEvent2(System::Object^  sender, System::EventArgs^  e)	//This is where 
                                                               //we publish the event to all subscribers
{
	this->myEvent2(this, e, this->textBox4->Text); // Raise the event using the EventDelegate signature
}

请注意,在这里我们添加string(要发布的)作为第三个参数,根据上面EventDelegate的声明。

我希望这个简单的例子能够帮助你解开事件、委托、发布者和订阅者的神秘面纱,就像它曾经帮助我一样。

主窗体

#pragma once

#include "Child Form 1.h"
#include "Child Form 2.h"

namespace PassingDataBetweenForms {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Collections::Generic;
    using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Diagnostics;

    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::Button^  button1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox3;

    private:
        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->button2 = (gcnew System::Windows::Forms::Button());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(30, 238);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(127, 27);
            this->button1->TabIndex = 0;
            this->button1->Text = L"Send";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::sendButton1);
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(173, 238);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(127, 27);
            this->button2->TabIndex = 1;
            this->button2->Text = L"Send";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::sendButton2);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(30, 37);
            this->textBox1->Multiline = true;
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(127, 79);
            this->textBox1->TabIndex = 2;
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(173, 37);
            this->textBox2->Multiline = true;
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(127, 79);
            this->textBox2->TabIndex = 3;
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(40, 13);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(106, 17);
            this->label1->TabIndex = 4;
            this->label1->Text = L"Data From Child 1";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(182, 13);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(108, 17);
            this->label2->TabIndex = 5;
            this->label2->Text = L"Data From Child 2";
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(189, 121);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(95, 17);
            this->label3->TabIndex = 19;
            this->label3->Text = L"Data To Child 2";
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(47, 121);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(93, 17);
            this->label4->TabIndex = 18;
            this->label4->Text = L"Data To Child 1";
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(173, 145);
            this->textBox4->Multiline = true;
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(127, 79);
            this->textBox4->TabIndex = 17;
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(30, 145);
            this->textBox3->Multiline = true;
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(127, 79);
            this->textBox3->TabIndex = 16;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(7, 17);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackColor = System::Drawing::Color::FromArgb
                (static_cast<System::Int32>(static_cast<System::Byte>(182)), 
                static_cast<System::Int32>(static_cast<System::Byte>(200)), 
                static_cast<System::Int32>(static_cast<System::Byte>(200)));
            this->ClientSize = System::Drawing::Size(331, 287);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Font = (gcnew System::Drawing::Font(L"Comic Sans MS", 9, 
                         System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
            this->MaximizeBox = false;
            this->MinimizeBox = false;
            this->Name = L"Form1";
            this->Text = L"Parent Form";
            this->Load += gcnew System::EventHandler(this, &Form1::load);
            this->Shown += gcnew System::EventHandler(this, &Form1::shown);
            this->ResumeLayout(false);
            this->PerformLayout();
        }
#pragma endregion
    private: ChildForm1^ c1;
    private: ChildForm2^ c2;

    private: System::Void load(System::Object^  sender, System::EventArgs^  e) {
        this->textBox3->Text = "hi child1 from parent";
        this->textBox4->Text = "hi child2 from parent";

        this->c1 = gcnew ChildForm1();
        this->c2 = gcnew ChildForm2();

        // The main form subscribes (as a listener) to the child form "send text" 
        // published events - these are the declarations for the event handlers shown below
        this->c1->myEvent1 += gcnew ChildForm1::EventDelegate1
         (this, &PassingDataBetweenForms::Form1::mySubscriber1a); // Events have only 2 args
        this->c1->myEvent2 += gcnew ChildForm1::EventDelegate2
         (this, &PassingDataBetweenForms::Form1::mySubscriber1b); // Events have only 2 args
        this->c2->myEvent1 += gcnew ChildForm2::EventDelegate1
         (this, &PassingDataBetweenForms::Form1::mySubscriber2a); // Events have only 2 args
        this->c2->myEvent2 += gcnew ChildForm2::EventDelegate2
         (this, &PassingDataBetweenForms::Form1::mySubscriber2b); // Events have only 2 args
    }
    private: System::Void shown(System::Object^  sender, System::EventArgs^  e) {
        this->c1->Show();
        this->c2->Show();
    }

    // The main form's subscriber event handlers which are declared above
    private: void mySubscriber1a(System::Object^  sender, 
          System::EventArgs^  e, String^ text) // From child form 1
    {
        this->textBox1->Text = text; // Here's where we display the message from the child1
    }
    private: void mySubscriber1b(System::Object^  sender, 
          System::EventArgs^  e, String^ text) // From child form 1
    {
        this->c2->SetTextBox2(text);  // Here's where we forward the message from child2 to child1
    }
    private: void mySubscriber2a(System::Object^  sender, 
         System::EventArgs^  e, String^ text) // From child form 2
    {
        this->textBox2->Text = text; // Here's where we display the message from the child2
    }
    private: void mySubscriber2b(System::Object^  sender, 
         System::EventArgs^  e, String^ text) // From child form 2
    {
        this->c1->SetTextBox2(text); // Here's where we forward the message from child1 to child2
    }

    private: System::Void sendButton1(System::Object^  sender, System::EventArgs^  e) {
        this->c1->SetTextBox1(this->textBox3->Text);
    }
    private: System::Void sendButton2(System::Object^  sender, System::EventArgs^  e) {
        this->c2->SetTextBox1(this->textBox4->Text);
    }
};
}

子窗体 1

#pragma once

namespace PassingDataBetweenForms {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Collections::Generic;
    using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Diagnostics;

    public ref class ChildForm1 : public System::Windows::Forms::Form
    {
    // Public events (using delegates) are declared here in order to publish strings 
    // for the subscriber (main form) who listens to these events
    public: delegate void EventDelegate1(System::Object^ sender, 
          System::EventArgs^ e, String^ message); // Publish an event (with a text message) 
                                                  // that others can subscribe to
    public: event EventDelegate1^ myEvent1;
    public: delegate void EventDelegate2(System::Object^ sender, 
             System::EventArgs^ e, String^ message); // Publish an event (with a text message) 
                                                     // that others can subscribe to
    public: event EventDelegate2^ myEvent2;

    public:
        ChildForm1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        ~ChildForm1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button1;

    private:
        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->label3 = (gcnew System::Windows::Forms::Label());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(189, 121);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(95, 17);
            this->label3->TabIndex = 29;
            this->label3->Text = L"Data To Sibling";
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(47, 121);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(93, 17);
            this->label4->TabIndex = 28;
            this->label4->Text = L"Data To Parent";
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(173, 145);
            this->textBox4->Multiline = true;
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(127, 79);
            this->textBox4->TabIndex = 27;
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(30, 145);
            this->textBox3->Multiline = true;
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(127, 79);
            this->textBox3->TabIndex = 26;
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(182, 13);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(108, 17);
            this->label2->TabIndex = 25;
            this->label2->Text = L"Data From Sibling";
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(40, 13);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(106, 17);
            this->label1->TabIndex = 24;
            this->label1->Text = L"Data From Parent";
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(173, 37);
            this->textBox2->Multiline = true;
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(127, 79);
            this->textBox2->TabIndex = 23;
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(30, 37);
            this->textBox1->Multiline = true;
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(127, 79);
            this->textBox1->TabIndex = 22;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(173, 238);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(127, 27);
            this->button2->TabIndex = 21;
            this->button2->Text = L"Send";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &ChildForm1::sendButton2);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(30, 238);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(127, 27);
            this->button1->TabIndex = 20;
            this->button1->Text = L"Send";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &ChildForm1::sendButton1);
            // 
            // ChildForm1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(7, 17);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackColor = System::Drawing::Color::FromArgb
                              (static_cast<System::Int32>(static_cast<System::Byte>(182)), 
                              static_cast<System::Int32>(static_cast<System::Byte>(200)), 
                static_cast<System::Int32>(static_cast<System::Byte>(200)));
            this->ClientSize = System::Drawing::Size(331, 287);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Font = (gcnew System::Drawing::Font(L"Comic Sans MS", 9));
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
            this->Name = L"ChildForm1";
            this->Text = L"ChildForm1";
            this->Load += gcnew System::EventHandler(this, &ChildForm1::load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    // My problem is how a child form can subscribe to another sibling's event - 
    // you need to construct an instance of the sibling & I don't know how to do that
    // So my current solution is to have the each child send a message to the parent, 
    // who in turn relays the message to its sibling.
    private: System::Void load(System::Object^  sender, System::EventArgs^  e)
    {
        this->textBox3->Text = "hi parent from child1";
        this->textBox4->Text = "hi child2 from child1";
    }
    
    public: void issueEvent1(System::Object^  sender, System::EventArgs^  e)    //This is where 
                                                          //we publish the event to all subscribers
    {
        this->myEvent1(this, e, this->textBox3->Text); // Raise the event using the EventDelegate 
                                                       // signature
    }
    public: void issueEvent2(System::Object^  sender, System::EventArgs^  e)    //This is where we 
                                                              //publish the event to all subscribers
    {
        this->myEvent2(this, e, this->textBox4->Text); // Raise the event using the 
                                                       // EventDelegate signature
    }
    
    public: void SetTextBox1(String^ string)
    {
        this->textBox1->Text = string;
    }
    public: void SetTextBox2(String^ string)
    {
        this->textBox2->Text = string;
    }
    
    private: System::Void sendButton1(System::Object^  sender, System::EventArgs^  e) {
        this->issueEvent1(sender, e);
    }
    private: System::Void sendButton2(System::Object^  sender, System::EventArgs^  e) {
        this->issueEvent2(sender, e);
    }
};
}

子窗体 2

#pragma once

namespace PassingDataBetweenForms {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Collections::Generic;
    using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Diagnostics;

    public ref class ChildForm2 : public System::Windows::Forms::Form
    {
    // Public events (using delegates) are declared here in order to publish strings 
    // for the subscriber (main form) who listens to these events
    public: delegate void EventDelegate1(System::Object^ sender, System::EventArgs^ e, 
      String^ message); // Publish an event (with a text message) that others can subscribe to
    public: event EventDelegate1^ myEvent1;
    public: delegate void EventDelegate2(System::Object^ sender, System::EventArgs^ e, 
      String^ message); // Publish an event (with a text message) that others can subscribe to
    public: event EventDelegate2^ myEvent2;

    public:
        ChildForm2(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        ~ChildForm2()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button1;

    private:
        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->label3 = (gcnew System::Windows::Forms::Label());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(189, 121);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(95, 17);
            this->label3->TabIndex = 29;
            this->label3->Text = L"Data To Sibling";
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(47, 121);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(93, 17);
            this->label4->TabIndex = 28;
            this->label4->Text = L"Data To Parent";
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(173, 145);
            this->textBox4->Multiline = true;
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(127, 79);
            this->textBox4->TabIndex = 27;
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(30, 145);
            this->textBox3->Multiline = true;
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(127, 79);
            this->textBox3->TabIndex = 26;
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(182, 13);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(108, 17);
            this->label2->TabIndex = 25;
            this->label2->Text = L"Data From Sibling";
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(40, 13);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(106, 17);
            this->label1->TabIndex = 24;
            this->label1->Text = L"Data From Parent";
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(173, 37);
            this->textBox2->Multiline = true;
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(127, 79);
            this->textBox2->TabIndex = 23;
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(30, 37);
            this->textBox1->Multiline = true;
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(127, 79);
            this->textBox1->TabIndex = 22;
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(173, 238);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(127, 27);
            this->button2->TabIndex = 21;
            this->button2->Text = L"Send";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &ChildForm2::sendButton2);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(30, 238);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(127, 27);
            this->button1->TabIndex = 20;
            this->button1->Text = L"Send";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &ChildForm2::sendButton1);
            // 
            // ChildForm2
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(7, 17);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackColor = System::Drawing::Color::FromArgb
                   (static_cast<System::Int32>(static_cast<System::Byte>(182)), 
                    static_cast<System::Int32>(static_cast<System::Byte>(200)), 
                static_cast<System::Int32>(static_cast<System::Byte>(200)));
            this->ClientSize = System::Drawing::Size(331, 287);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Font = (gcnew System::Drawing::Font(L"Comic Sans MS", 9));
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->Margin = System::Windows::Forms::Padding(3, 4, 3, 4);
            this->Name = L"ChildForm2";
            this->Text = L"ChildForm2";
            this->Load += gcnew System::EventHandler(this, &ChildForm2::load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    // My problem is how a child form can subscribe to another sibling's event - you need to 
    // construct an instance of the sibling & I don't know how to do that
    // So my current solution is to have the each child send a message to the parent, 
    // who in turn relays the message to its sibling.
    private: System::Void load(System::Object^  sender, System::EventArgs^  e)
    {
        this->textBox3->Text = "hi parent from child2";
        this->textBox4->Text = "hi child1 from child2";
    }

    public: void issueEvent1(System::Object^  sender, System::EventArgs^  e)    //This is where we 
                                                            //publish the event to all subscribers
    {
        this->myEvent1(this, e, this->textBox3->Text); // Raise the event using the EventDelegate 
                                                       // signature
    }
    public: void issueEvent2(System::Object^  sender, System::EventArgs^  e)    //This is where we 
                                                            //publish the event to all subscribers
    {
        this->myEvent2(this, e, this->textBox4->Text); // Raise the event using the EventDelegate 
                                                       // signature
    }

    public: void SetTextBox1(String^ string)
    {
        this->textBox1->Text = string;
    }
    public: void SetTextBox2(String^ string)
    {
        this->textBox2->Text = string;
    }

    private: System::Void sendButton1(System::Object^  sender, System::EventArgs^  e) {
        this->issueEvent1(sender, e);
    }
    private: System::Void sendButton2(System::Object^  sender, System::EventArgs^  e) {
        this->issueEvent2(sender, e);
    }
};
}
© . All rights reserved.