events and delegates accessing from another class

  • Thread starter Thread starter Asfar
  • Start date Start date
A

Asfar

Here is my problem:

In file form1.h I have the following code:
#pragma once
#include "Test.h"
namespace AccessCheck
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public delegate void UpdateStatusEventHandler(String^);

public ref class Form1 : public System::Windows::Forms::Form
{
static event UpdateStatusEventHandler ^E;

public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
E += gcnew UpdateStatusEventHandler(this,
&Form1::OnUpdateStatus);
}

public:
System::Void OnUpdateStatus(String ^str)
{
MessageBox::Show(str);
}


private:
System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
Test ^test = gcnew Test();
test->FireEvents();
}
};
}


In the File Test1.h I have the following code:
#pragma once
using namespace System;
namespace AccessCheck
{
ref class Test
{
public:
Test(void)
{
}

public:
void FireEvents()
{
// I want to call OnUpdateStatus in Form1.h
}
};
}

In the funtion FireEvents() I want to do some processing and based on the
processing state I want to call UpdateStatusEventHandler. Any Idea on how to
do this.

Thanks,
-Asfar
 
In the funtion FireEvents() I want to do some processing and based on the
processing state I want to call UpdateStatusEventHandler. Any Idea on how to
do this.

If I understand your question correctly, you want to let your test class
raise an event from your Form class?

You cannot raise that event directly (see C3767)
The easiest way would be to make a public function in Form1 that raises the
event. see my example:

public delegate void SomeEventHandler(void);
ref class EC
{
public:
EC()
{
}
event SomeEventHandler^ OnEvent;
void DoSomethingThatRaisesAnEvent(void)
{
OnEvent();
}
};

void LocalEventHandler(void)
{
MessageBox::Show("Tada");
}

int main(array<System::String ^> ^args)
{
EC ^ anEc = gcnew EC();
anEc->OnEvent += gcnew SomeEventHandler(LocalEventHandler);
anEc->DoSomethingThatRaisesAnEvent();
return 0;
}

This allows you to raise EC events from anywhere.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top