Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

events and delegates accessing from another class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Asfar - 05 Apr 2006 22:19 GMT
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
Bruno van Dooren - 06 Apr 2006 08:35 GMT
> 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.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.