Often when you create a new class you need to provide some method for the new class to pass back information to the original class, or trigger events in the original class. You can have the original class check properties in the new class, but thats no good for events that get generated in the new class which need to callback.

In the original class

	//***************************************
	//***************************************
	//********** CALLBACK FUNCTION **********
	//***************************************
	//***************************************
	public: void MyCallbackFunction (MyClass ^%PassedClass)
	{
		ClassBeingWorkedOn = PassedClass;
	}

	//----- IN A FUNCTION THAT WANTS TO PASS THE CALLBACK FUNCTION -----
	MyDelegateHandler ^del = gcnew MyDelegateHandler(this, &frmMain::MyCallbackFunction);

	frmCellSetup ^ConfigForm = gcnew frmCellSetup(del, ClassBeingWorkedOn);
	ConfigForm->ShowDialog();


//If your callback function was static you need to use this 1 argument version instead:
//	MyDelegateHandler ^del = gcnew MyDelegateHandler(&this->MyCallbackFunction);
//and if so you will also need to ensure that anything it will modify are also static

Note that in this example you can ignore MyClass if you want – it an example of how to pass a class as part of a callback but you don’t need it if you don’t want to do this.

In the new class being created that will call back (i.e. a sub form)


//----- Add delegate definition in the new class namespace -----
namespace MyNamespace {
	public delegate void MyDelegateHandler (MyClass ^%PassedClass);

//You generally don't need this, but if for some reason you need to declare the event inside a class (inside the 'public ref class'):
//public: event MyDelegateHandler ^del;

	//*********************************
	//*********************************
	//********** CONSTRUCTOR **********
	//*********************************
	//*********************************
	frmCellSetup(MyDelegateHandler ^del, MyClass ^%ClassBeingWorkedOn)
	{
		InitializeComponent();
		
		ClassBeingWorkedOnLive = ClassBeingWorkedOn;	//Store handle to class we're going to modify
		SourceClassCallbackFunction = del;		//Store callback fucntion
	}

	//GLOBAL DEFINITIONS
	private: MyDelegateHandler ^SourceClassCallbackFunction;
	private: MyClass ^ClassBeingWorkedOnLive;

	//----- WHEN YOU WANT TO CALL THE CALLBACK FUNCTION -----
	SourceClassCallbackFunction(ClassBeingWorkedOnLive);

If you need to have more than 1 other class call the source class callback function then simply create different MyDelegateHandler’s in each class.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.