473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

events and delegates accessing from another class

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
Apr 5 '06 #1
1 1527
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
6
by: Flare | last post by:
Hi i have a qusstion about events and delegates. Especially the precis role of the Event. Eg. We have a class wich want to fire events so we declare: public delegate void...
15
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
14
by: Mark Allison | last post by:
Hi, I am new to C# and want to create my own events within a console app. However I am struggling mentally with this, and have looked at the MSDN documentation but I am drawing a blank. Is there...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.