473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;

public delegate void UpdateStatusEve ntHandler(Strin g^);

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

public:
Form1(void)
{
InitializeCompo nent();
//
//TODO: Add the constructor code here
//
E += gcnew UpdateStatusEve ntHandler(this,
&Form1::OnUpdat eStatus);
}

public:
System::Void OnUpdateStatus( String ^str)
{
MessageBox::Sho w(str);
}
private:
System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ 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 UpdateStatusEve ntHandler. Any Idea on how to
do this.

Thanks,
-Asfar
Apr 5 '06 #1
1 1552
> In the funtion FireEvents() I want to do some processing and based on the
processing state I want to call UpdateStatusEve ntHandler. 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 SomeEventHandle r(void);
ref class EC
{
public:
EC()
{
}
event SomeEventHandle r^ OnEvent;
void DoSomethingThat RaisesAnEvent(v oid)
{
OnEvent();
}
};

void LocalEventHandl er(void)
{
MessageBox::Sho w("Tada");
}

int main(array<Syst em::String ^> ^args)
{
EC ^ anEc = gcnew EC();
anEc->OnEvent += gcnew SomeEventHandle r(LocalEventHan dler);
anEc->DoSomethingTha tRaisesAnEvent( );
return 0;
}

This allows you to raise EC events from anywhere.

--

Kind regards,
Bruno.
br************* *********@hotma il.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
9201
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
6
1351
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 TestEventHandler(object sender, EventArgs arg); public event TestEventHandler Test; And fire the event with
15
2101
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 reference to some of the ClassA objects created in ClassB. In ClassC I hook into the ClassA events with a foreach loop so that I hook each object. The code is something like this: class ClassC { void SomeMethod() { foreach (ClassA item in...
14
1493
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 anywhere on the net that shows a very basic delegate and a very basic event? Some nice simple example code is what I'm looking for with no fluff around it. Thanks!
4
22894
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 when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
11
1887
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 mean, for one, delegates point to subs, so when you call a delegate, why not just call the sub dierectly and not bother adding the extra code involved adding the delegate?
30
3662
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
15
2614
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, the main thread waits. At the completion of each subthread, the mainthread checks all 12 thread objects to see if they are done. If they are, raise an event that says we're done. So, it's kinda like this: ProcessThread - Creates a ProcessObject
9
1838
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using CSharpLib;
2
2347
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 fires events when the data changes, is often the central part. Connected to these states are various observers that act on changes in data, by altering the information presented to the user, executing code and so on, each observer with its own...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.